Forum rules - please read before posting.

Custom Action - Open URL

edited November 2021 in Extending the editor

Just put this simple action together which takes a URL and then will do Unity's Application.OpenURL() to open the URL in whatever web browser is your system default:

Just paste this into a script called ActionOpenURL:

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace AC
{
    [System.Serializable]
    public class ActionOpenURL : Action
    {
        // Declare variables here
        public string url;

        public ActionOpenURL()
        {
            this.isDisplayed = true;
            category = ActionCategory.Custom;
            title = "Open URL";
            description = "Opens a URL using Application.OpenURL()";
        }

        override public float Run ()
        {
            Application.OpenURL(url);
            Debug.Log("URL opened");

            return 0f;
        }

        #if UNITY_EDITOR

        override public void ShowGUI ()
        {
            // Action-specific Inspector GUI code her
            url = EditorGUILayout.TextField("URL to open:", url);

            AfterRunningOption ();
        }
        #endif
    }
}

Comments

  • Looking good! One for the wiki, perhaps?

    Quick note: with the updated API in v1.73.0, it's recommended to declare an Action's characteristics through properties rather than serializable fields in its Constructor. For the moment, the old format will work, but the newer one is a little cleaner:

    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
        [System.Serializable]
        public class ActionOpenURL : Action
        {
    
            public string url;
    
            public override string Title => "Open URL";
            public override ActionCategory Category => ActionCategory.Custom;
            public override string Description => "Opens a URL using Application.OpenURL()";
    
            override public float Run ()
            {
                Application.OpenURL(url);
                Debug.Log("URL opened");
                return 0f;
            }
    
            #if UNITY_EDITOR
    
            override public void ShowGUI ()
            {
                url = EditorGUILayout.TextField("URL to open:", url);
            }
    
            #endif
        }
    }
    
  • @ChrisIceBox I was hoping you'd chime in with how it could be improved. Custom actions aren't my forte, so all feedback is good feedback. I'll give your updated script a whirl.

  • edited November 2021

    Works fine! I did a bit of modification for the text entry:

    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
        [System.Serializable]
        public class ActionOpenURL : Action
        {
            public string url;
    
            public override string Title => "Open URL";
            public override ActionCategory Category => ActionCategory.Custom;
            public override string Description => "Opens a URL using Application.OpenURL()";
    
            override public float Run()
            {
                Application.OpenURL(url);
                Debug.Log("URL opened");
                return 0f;
            }
    
    #if UNITY_EDITOR
    
            override public void ShowGUI()
            {
                EditorStyles.textField.wordWrap = true;
                url = EditorGUILayout.TextField("URL to open:", url, GUILayout.MinHeight(100));
            }
    
    #endif
        }
    }
    

    This gives a bit more space to see a long URL and also wraps the text:

  • Thank you for taking the time to write and post this!

  • Just implemented this and works great. Thanks!

  • Where do we put this script file to get it to show up under custom actions?

  • edited August 2022

    @Endlessdescent It depends on what folder you have setup to be the home of custom actions. This part of the manual explains it.

  • edited August 2022

    Sorry to bother again, but have attached this script to a button on my title screen. It shows correctly in the action list, but when you press the button in game, absolutely nothing happens. What did I do wrong here?

  • edited August 2022

    @Endlessdescent If you put a different action in the action list, can you confirm the action list works?

  • edited August 2022

    @Temmy Yes, but good question. I swapped the action with starting a new game and it works fine. I'll try deleting the action and script then starting over in case I messed something up...
    Edit: So deleting everything and starting over worked fine. Thank you for the helpful script!
    I think the problem is that I probably incorrectly named/deleted the script on my first try. This seems to cause issues with the build's info, so always make sure to delete and load assets properly :)

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.