HutongGames.PlayMaker; //需要引用這個命名空間
紅色的字體是對變量的操作,其他的沒啥關系.
#region 判斷為 PlayMakerFSM 組件時 if (behaviour.GetType() == typeof(PlayMakerFSM)) { PlayMakerFSM temp_PlayMakerFSM = behaviour as PlayMakerFSM; compKey = temp_PlayMakerFSM.Fsm.Name;//拿到狀態機的名字,如 FSM01 #region 操作 float 類型的變量(其他類型的變量也類似) //拿到 PlayMakerFSM組件 的所有Float類型的變量值,存在數組里 NamedVariable[] floatArray = temp_PlayMakerFSM.FsmVariables.GetNamedVariables(VariableType.Float); //Debug.Log("floatArray長度:" + floatArray.Length); foreach (var variable in floatArray) { //判斷變量是否是 inspector 的,是的話再判斷是什么類型的,存入相應的字典里 if (variable.ShowInInspector == true) { //得到變量的要存到字典的Value (variable.RawValue 也可以拿到變量值,不過應該是FsmFloat類型的)
float variableValue = float.Parse(variable.ToString());//拿到變量的值(它是FsmFloat類型的,要先轉換為string,再轉為float)
//Vector3也是一樣的,它是FsmVector3類型的,先轉為string再轉為Vector3(vector3與string轉換方法,http://www.cnblogs.com/Peng18233754457/p/8653663.html)
//Quaternion也是一樣的,網上有string與Quaternion的轉換方法,http://www.cnblogs.com/Peng18233754457/p/8763137.html
//((FsmFloat)variable).Value = 1.23;//改變變量的值 //得到變量的要存到字典的Key (變量的名稱) string variableKey = variable.Name;//拿到變量的名稱 //字典里不包含這個鍵,就直接加進去 if (!float_TempDic.ContainsKey(variableKey)) { float_TempDic.Add(variableKey, variableValue); } //字典里包含這個鍵,就改掉這個鍵對應的值 else { float_TempDic[variableKey] = variableValue; } //當這個字典里有東西的時候 if (float_TempDic.Keys.Count != 0) { //加到外層字典 if (!describePath.comp_FloatVar_Val.ContainsKey(compKey)) { //describePath.comp_FloatVar_Val.Add(compKey, float_TempDic); describePath.comp_FloatVar_Val.Add(compKey, JsonUtility.ToJson(new Serialization<string, float>(float_TempDic))); } else { describePath.comp_FloatVar_Val[compKey] = JsonUtility.ToJson(new Serialization<string, float>(float_TempDic)); } float_TempDic.Clear(); } } }