浏览代码

Attributes for State Machines / Scenes

/main
Thomas ICHÉ 6 年前
当前提交
910a33c3
共有 10 个文件被更改,包括 153 次插入8 次删除
  1. 29
      Editor/PropertyDrawers/ScenePropertyDrawer.cs
  2. 2
      Runtime/Actions/StreamingLevelAction.cs
  3. 2
      Runtime/StateMachine/SetStateAction.cs
  4. 9
      Runtime/StateMachine/StateMachine.cs
  5. 60
      Editor/PropertyDrawers/StateMachineStatePropertyDrawer.cs
  6. 11
      Editor/PropertyDrawers/StateMachineStatePropertyDrawer.cs.meta
  7. 9
      Runtime/PropertyAttributes/SceneAttribute.cs
  8. 11
      Runtime/PropertyAttributes/SceneAttribute.cs.meta
  9. 17
      Runtime/PropertyAttributes/StateMachineStateAttribute.cs
  10. 11
      Runtime/PropertyAttributes/StateMachineStateAttribute.cs.meta

29
Editor/PropertyDrawers/ScenePropertyDrawer.cs


using UnityEngine.SceneManagement;
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using System.Linq;
[CustomPropertyDrawer(typeof(Scene))]
[CustomPropertyDrawer(typeof(SceneAttribute))]
EditorGUI.ObjectField(position, property);
}
int count = EditorSceneManager.sceneCountInBuildSettings;
string[] sceneNames = new string[count];
GUIContent[] displayedOptions = new GUIContent[count];
int[] values = new int[count];
string currentValue = property.stringValue;
int selectedIndex = -1;
for (int i = 0; i < sceneNames.Length; i++)
{
sceneNames[i] = SceneUtility.GetScenePathByBuildIndex(i).Split(new char[] { '\\', '/' }).Last().Replace(".unity", "");
displayedOptions[i] = new GUIContent(sceneNames[i]);
values[i] = i;
if (currentValue == sceneNames[i])
selectedIndex = i;
}
int newVal = EditorGUI.IntPopup(position, new GUIContent(property.displayName), selectedIndex, displayedOptions, values);
if (GUI.changed)
{
property.stringValue = sceneNames[newVal];
}
}
}
}

2
Runtime/Actions/StreamingLevelAction.cs


{
public class StreamingLevelAction : ActionBase
{
[ReorderableList]
[ReorderableList, Scene]
public string[] Scenes;
public string SceneToActivate;
public LevelStreamingManager.StreamingAction Action = LevelStreamingManager.StreamingAction.Load;

2
Runtime/StateMachine/SetStateAction.cs


set { m_State = value; }
}
[SerializeField]
[SerializeField, StateMachineState("StateMachine")]
protected string m_State = "State";
public override void Execute(GameObject instigator = null)

9
Runtime/StateMachine/StateMachine.cs


{
public class StateMachine : MonoBehaviour
{
public State DefaultState;
[StateMachineState]
public string DefaultState;
[ReorderableList]
public State[] States;

foreach (var state in States)
state.gameObject.SetActive(false);
SetState(DefaultState.StateName);
SetState(DefaultState);
State newState = States.First(o => o.StateName == stateName);
State newState = States.FirstOrDefault(o => o.StateName == stateName);
if(newState != null)
{

// Finally, call State enter
Callable.Call(m_CurrentState.OnStateEnter, gameObject);
}
else
Debug.LogWarning(string.Format("{0} : Trying to set unknown state {1}", gameObject.name, stateName), gameObject);
}
public void Update()

60
Editor/PropertyDrawers/StateMachineStatePropertyDrawer.cs


using UnityEngine.SceneManagement;
using UnityEngine;
using UnityEditor;
using GameplayIngredients.StateMachines;
namespace GameplayIngredients.Editor
{
[CustomPropertyDrawer(typeof(StateMachineStateAttribute))]
public class StateMachineStatePropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
string propertyName = (attribute as StateMachineStateAttribute).PropertyName;
StateMachine stateMachine;
if (propertyName == "")
stateMachine = (StateMachine)property.serializedObject.targetObject;
else
stateMachine = (StateMachine)property.serializedObject.FindProperty(propertyName).objectReferenceValue;
if (stateMachine == null)
{
EditorGUI.HelpBox(position, "StateMachineStateAttribute property parameter references a null or not a GameplayIngredients.StateMachines.StateMachine type", MessageType.Error);
}
else
{
string currentValue = property.stringValue;
int count = stateMachine.States.Length;
int[] indices = new int[count];
GUIContent[] labels = new GUIContent[count];
int selected = -1;
for(int i = 0; i< count; i++)
{
string stateName = stateMachine.States[i].StateName;
if (stateName == currentValue)
selected = i;
indices[i] = i;
labels[i] = new GUIContent(stateName);
}
int newIdx = EditorGUI.IntPopup(position, new GUIContent(property.displayName), selected, labels, indices);
if(GUI.changed)
{
property.stringValue = stateMachine.States[newIdx].StateName;
}
}
}
static class Styles
{
public static GUIStyle errorfield;
static Styles()
{
errorfield = new GUIStyle(EditorStyles.objectField);
}
}
}
}

11
Editor/PropertyDrawers/StateMachineStatePropertyDrawer.cs.meta


fileFormatVersion: 2
guid: b9ead8a31655f214b8768b02688a41cf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

9
Runtime/PropertyAttributes/SceneAttribute.cs


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace GameplayIngredients
{
public class SceneAttribute : PropertyAttribute { }
}

11
Runtime/PropertyAttributes/SceneAttribute.cs.meta


fileFormatVersion: 2
guid: 4d6bacd85ad9374418dcdc6b1768b95c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

17
Runtime/PropertyAttributes/StateMachineStateAttribute.cs


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace GameplayIngredients
{
public class StateMachineStateAttribute : PropertyAttribute
{
public readonly string PropertyName;
public StateMachineStateAttribute(string propertyName = "")
{
PropertyName = propertyName;
}
}
}

11
Runtime/PropertyAttributes/StateMachineStateAttribute.cs.meta


fileFormatVersion: 2
guid: 3f6c4fc5e5b9fe44c8ca902bcc94b4bf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存