Forum rules - please read before posting.

Trying to make a custom action

Hey everyone,

I've been trying to make a simple custom action for changing a text color in a UnityUI menu. But I'm not sure if it's going through or if I'm doing it properly? I have text that changes color depending on the menu selection you're on.

E.g. "Options" Text turns green if it's the one selected and black if you select a different option.

I set the text field and color in the action, but when I start the game and the action to turn the text green runs, it says black. But when I run the game again, that action seems to change the text to green immediately (not when the action is run). Then when I use another actionlist to set the color to black again, again nothing happens, but if I restart the game it sets to black, etc. It's like its not updating at runtime but its saving in the prefab or something. I'm really confused and not sure what I'm doing wrong?

Here's a pastebin of my custom action
https://pastebin.com/irW6a0XS

Comments

  • It's a case of your Action affecting the original prefab instead of the runtime instance - so you're not seeing the changes reflected until your game is restarted.

    To have the Action affect the scene instance of a given prefab, you'll need to have it record that prefab's Constant ID and use that to locate the object at runtime.

    You're also using the old Action format - constructors are no longer required.

    A series of tutorials that show how to write custom Actions - including the use of Constant ID values - can be found here.

  • Thank you so much Chris, the ConstantID was the problem!

    Just out of curiosity, I tried to make the color change fade with Color.Lerp but it doesn't seem to work - not sure if this is a limit of the AC GUI? No worries if it is just thought it would look nicer hahaha

    https://pastebin.com/bAS8aSuW

    I tried to add fadeColor into the GUI part and it threw errors at me so I guess not lol

  • If you want an Action to be run over time, you need to set isRunning to true and return a non-zero value in the Run function. This will cause AC to keep running its Run function until the opposite is true - see the comments in the ActionTemplate script's Run function for details.

    Try this:

    using UnityEngine;
    using UnityEngine.UI;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
    
        [System.Serializable]
        public class ActionChangeFontColor : Action
        {
    
            public Text mytext;
            public Color mycolor = Color.white;
            private Color startColor;
            public int constantID;
            public float fadeTime = 2;
            private float fadeStart = 0;
    
            public override ActionCategory Category { get { return ActionCategory.Custom; }}
            public override string Title { get { return "Change Font Color"; }}
            public override string Description { get { return "It changes the font color I guess."; }}
    
    
            override public float Run ()
            {
                if (!isRunning)
                {
                    if (fadeTime <= 0f)
                    {
                        mytext.color = mycolor;
                        return 0f;
                    }
    
                    isRunning = true;
                    startColor = mytext.color;
                    fadeStart = 0;
                }
    
                fadeStart += Time.deltaTime;
                if (fadeStart >= fadeTime)
                {
                    mytext.color = mycolor;
                    isRunning = false;
                    return 0f;
                }
    
                float t = fadeStart / fadeTime;
                mytext.color = Color.Lerp (startColor, mycolor, t);
                return defaultPauseTime;
            }
    
    
            override public void AssignValues ()
            {
                mytext = AssignFile (constantID, mytext);
            }
    
    
            #if UNITY_EDITOR
    
            public override void ShowGUI ()
            {   
                mytext = (Text) EditorGUILayout.ObjectField ("Text:", mytext, typeof (Text), false);
                mycolor = (Color) EditorGUILayout.ColorField ("Color:", mycolor);
                constantID = FieldToID (mytext, constantID);
                mytext = IDToField (mytext, constantID, true);
                fadeTime = EditorGUILayout.FloatField ("Fade time (s):", fadeTime);
            }
    
            #endif
    
        }
    
    }
    
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.