可以扩展Action以使用ActionList参数,以便在运行时设置字段值。在本教程中,我们将为自定义Action提供接受GameObject参数代替GUI中的字段的能力。
参数涉及使用Lists
,这意味着我们必须首先引入一个新库。在脚本的标题中,添加以下内容:
1
| using System.Collections.Generic;
|
我们将参数存储为ID号,这与记录Constant ID的方式非常相似。但是,我们必须确保默认情况下将其设置为-1。添加到声明变量列表:
1
| public int parameterID = -1;
|
必须修改ShowGUI函数的重载以接受参数列表。
1
| override public void ShowGUI (List<ActionParameter> parameters)
|
然后,我们将调用一个函数,该函数将显示参数选择器(如果有)。如果选择其中一个,我们将重置其他变量-否则,我们将正常显示GUI:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public override void ShowGUI (List<ActionParameter> parameters) {
parameterID = Action.ChooseParameterGUI("GameObject to affect: ", parameters, parameterID, ParameterType.GameObject); if (parameterID > 0) { constantID = 0; objectToAffect = null; } else { objectToAffect = (GameObject)EditorGUILayout.ObjectField("GameObject to affect:", objectToAffect, typeof(GameObject), true); newGravityState = EditorGUILayout.Toggle("New gravity state:", newGravityState);
constantID = FieldToID(objectToAffect, constantID); objectToAffect = IDToField(objectToAffect, constantID, true); } }
|
最后,我们将更新AssignValues
函数以接受参数,并根据需要重新分配游戏对象:
1 2 3 4
| override public void AssignValues (List<ActionParameter> parameters) { objectToAffect = AssignFile (parameters, parameterID, constantID, objectToAffect); }
|
我们的动作现在已经完成了——它将与GameObject、Prefab和ActionListParameter一起工作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| using UnityEngine; using System.Collections.Generic; #if UNITY_EDITOR using UnityEditor; #endif
namespace AC {
[System.Serializable] public class ActionGravity : Action { public override ActionCategory Category { get { return ActionCategory.Object; }} public override string Title { get { return "Change gravity"; }} public override string Description { get { return "Enables or disables a Rigidbody's Gravity checkbox"; }}
public GameObject objectToAffect; public bool newGravityState; public int constantID; public int parameterID = -1; public override float Run () { if (objectToAffect && objectToAffect.GetComponent<Rigidbody>()) { objectToAffect.GetComponent<Rigidbody>().useGravity = newGravityState; } if (!isRunning) { isRunning = true; return defaultPauseTime; } else { isRunning = false; return 0f; } }
public override void Skip () { Run (); }
public override void AssignValues(List<ActionParameter> parameters) { objectToAffect = AssignFile(parameters, parameterID, constantID, objectToAffect); }
#if UNITY_EDITOR
public override void ShowGUI (List<ActionParameter> parameters)
parameterID = Action.ChooseParameterGUI("GameObject to affect: ", parameters, parameterID, ParameterType.GameObject); if (parameterID > 0) { constantID = 0; objectToAffect = null; } else { objectToAffect = (GameObject)EditorGUILayout.ObjectField("GameObject to affect:", objectToAffect, typeof(GameObject), true); newGravityState = EditorGUILayout.Toggle("New gravity state:", newGravityState);
constantID = FieldToID(objectToAffect, constantID); objectToAffect = IDToField(objectToAffect, constantID, true); } }
public override string SetLabel () { return string.Empty; }
#endif }
}
|