Adventure Creator Wikia

This custom Action lets you change a character's expression from a dropdown list, without having to enter it as a token into speech text.

To use it:

  1. Copy/paste the code below into a new C# file named ActionCharExpression.
  2. Move the file to a separate "custom actions" folder in your Project window
  3. Click the folder icon inside the Custom Actions panel at the top of the Actions Manager and point to this new folder
  4. The Character: Set expression Action should now be available in ActionLists.

ActionCharExpression.cs:

using UnityEngine;
using System.Collections.Generic;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace AC
{

    [System.Serializable]
    public class ActionCharExpression : Action
    {

        public bool isPlayer;
        public int playerID = -1;

        public int _charID = 0;
        public Char _char;
        protected Char runtimeChar;

        public string expression;
        public bool clearExpression;

        public override ActionCategory Category { get { return ActionCategory.Character; }}
        public override string Title { get { return "Set expression"; }}

        public override void AssignValues (List<ActionParameter> parameters)
        {
            if (isPlayer)
            {
                runtimeChar = AssignPlayer (playerID, parameters, -1);
            }
            else
            {
                runtimeChar = AssignFile <Char> (_charID, _char);
            }
        }

        public override float Run ()
        {
            if (runtimeChar)
            {
				if (clearExpression)
				{
					runtimeChar.ClearExpression ();
				}
				else
				{
                	runtimeChar.SetExpression (expression);
                }
            }
            LogWarning ("Character not found");
            return 0f;
        }

        #if UNITY_EDITOR

        public override void ShowGUI (List<ActionParameter> parameters)
        {
        	Char editChar = null;

            isPlayer = EditorGUILayout.Toggle ("Is Player?", isPlayer);
            if (isPlayer)
            {
                if (KickStarter.settingsManager != null && KickStarter.settingsManager.playerSwitching == PlayerSwitching.Allow)
                {
                    playerID = ChoosePlayerGUI (playerID, true);
                    PlayerPrefab playerPrefab = KickStarter.settingsManager.GetPlayerPrefab (playerID);
                    if (playerPrefab != null) editChar = playerPrefab.playerOb;
                }
                else
                {
                	Player player = FindObjectOfType<Player> ();
                	if (player) editChar = player;
                	else if (KickStarter.settingsManager) player = KickStarter.settingsManager.player;
                }
            }
            else
            {
                _char = (Char) EditorGUILayout.ObjectField ("Character:", _char, typeof (Char), true);
                _charID = FieldToID <Char> (_char, _charID);
                _char = IDToField <Char> (_char, _charID, true);
                editChar = _char;
            }

            clearExpression = EditorGUILayout.Toggle ("Clear expression?", clearExpression);
            if (clearExpression) return;

            if (editChar && editChar.expressions != null && editChar.useExpressions && editChar.expressions.Count > 0)
            {
            	string[] expressionNames = new string[editChar.expressions.Count];
            	int index = 0;
            	for (int i = 0; i < expressionNames.Length; i++)
            	{
            		expressionNames[i] = editChar.expressions[i].label;
            		if (expressionNames[i] == expression) index = i;
            	}

            	index = EditorGUILayout.Popup ("Expression:", index, expressionNames);
            	expression = expressionNames[index];
            	Texture preview = editChar.expressions[index].portraitIcon.texture;
				CustomGUILayout.ObjectField <Texture> (preview, false, GUILayout.Width (70), GUILayout.Height (70));
            }
            else
            {
            	expression = EditorGUILayout.TextField ("Expression:", expression);
            }
        }

        #endif

    }

}