You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
186 lines
4.0 KiB
186 lines
4.0 KiB
10 years ago
|
using UnityEditor;
|
||
10 years ago
|
using UnityEditorInternal;
|
||
10 years ago
|
using UnityEngine;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
|
||
10 years ago
|
namespace Fungus.Script
|
||
10 years ago
|
{
|
||
|
|
||
10 years ago
|
[CustomEditor (typeof(Command), true)]
|
||
|
public class CommandEditor : Editor
|
||
10 years ago
|
{
|
||
10 years ago
|
public static Command selectedCommand;
|
||
10 years ago
|
|
||
10 years ago
|
void OnEnable()
|
||
|
{
|
||
10 years ago
|
Command t = target as Command;
|
||
10 years ago
|
if (t != null)
|
||
|
{
|
||
|
t.hideFlags = HideFlags.HideInInspector;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
public static CommandInfoAttribute GetCommandInfo(System.Type commandType)
|
||
|
{
|
||
|
object[] attributes = commandType.GetCustomAttributes(typeof(CommandInfoAttribute), false);
|
||
|
foreach (object obj in attributes)
|
||
|
{
|
||
|
CommandInfoAttribute commandInfoAttr = obj as CommandInfoAttribute;
|
||
|
if (commandInfoAttr != null)
|
||
|
{
|
||
|
return commandInfoAttr;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
|
||
10 years ago
|
public virtual void DrawCommandInspectorGUI()
|
||
|
{
|
||
10 years ago
|
Command t = target as Command;
|
||
10 years ago
|
if (t == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
FungusScript fungusScript = t.GetFungusScript();
|
||
10 years ago
|
|
||
10 years ago
|
CommandInfoAttribute commandInfoAttr = CommandEditor.GetCommandInfo(t.GetType());
|
||
10 years ago
|
if (commandInfoAttr == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
10 years ago
|
GUILayout.BeginVertical(GUI.skin.box);
|
||
|
|
||
|
GUI.backgroundColor = Color.green;
|
||
10 years ago
|
GUILayout.BeginHorizontal(GUI.skin.button);
|
||
10 years ago
|
|
||
10 years ago
|
string commandName = commandInfoAttr.CommandName;
|
||
10 years ago
|
GUIStyle commandStyle = new GUIStyle(EditorStyles.miniButton);
|
||
10 years ago
|
if (t.enabled)
|
||
|
{
|
||
10 years ago
|
if (fungusScript.colorCommands)
|
||
|
{
|
||
|
GUI.backgroundColor = t.GetButtonColor();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
GUI.backgroundColor = Color.white;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
GUI.backgroundColor = Color.grey;
|
||
|
}
|
||
10 years ago
|
|
||
|
bool enabled = t.enabled;
|
||
|
|
||
|
if (GUILayout.Button(commandName, commandStyle, GUILayout.MinWidth(80), GUILayout.ExpandWidth(true)))
|
||
|
{
|
||
|
enabled = !enabled;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
GUI.backgroundColor = Color.white;
|
||
10 years ago
|
enabled = GUILayout.Toggle(enabled, new GUIContent());
|
||
10 years ago
|
|
||
10 years ago
|
if (t.enabled != enabled)
|
||
|
{
|
||
|
Undo.RecordObject(t, "Set Enabled");
|
||
|
t.enabled = enabled;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
if (fungusScript != null)
|
||
|
{
|
||
10 years ago
|
if (GUILayout.Button("Copy", EditorStyles.miniButton))
|
||
10 years ago
|
{
|
||
10 years ago
|
fungusScript.copyCommand = t;
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
GUILayout.EndHorizontal();
|
||
10 years ago
|
GUI.backgroundColor = Color.white;
|
||
10 years ago
|
|
||
10 years ago
|
EditorGUILayout.Separator();
|
||
|
|
||
|
DrawCommandGUI();
|
||
10 years ago
|
|
||
10 years ago
|
EditorGUILayout.Separator();
|
||
10 years ago
|
|
||
10 years ago
|
if (t.errorMessage.Length > 0)
|
||
|
{
|
||
|
GUIStyle style = new GUIStyle(GUI.skin.label);
|
||
|
style.normal.textColor = new Color(1,0,0);
|
||
|
EditorGUILayout.LabelField(new GUIContent("Error: " + t.errorMessage), style);
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
GUILayout.EndVertical();
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
public virtual void DrawCommandGUI()
|
||
10 years ago
|
{
|
||
|
DrawDefaultInspector();
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
static public Command PasteCommand(Command copyCommand, Sequence sequence)
|
||
10 years ago
|
{
|
||
10 years ago
|
System.Type type = copyCommand.GetType();
|
||
|
Component copy = Undo.AddComponent(sequence.gameObject, type);
|
||
10 years ago
|
System.Reflection.FieldInfo[] fields = type.GetFields();
|
||
|
foreach (System.Reflection.FieldInfo field in fields)
|
||
|
{
|
||
10 years ago
|
field.SetValue(copy, field.GetValue(copyCommand));
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
FungusScript fungusScript = sequence.GetFungusScript();
|
||
|
|
||
|
Undo.RecordObject(fungusScript, "Paste Command");
|
||
|
|
||
10 years ago
|
Command newCommand = copy as Command;
|
||
10 years ago
|
sequence.commandList.Add(newCommand);
|
||
|
|
||
|
return newCommand;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
static public void ObjectField<T>(SerializedProperty property, GUIContent label, GUIContent nullLabel, List<T> objectList) where T : MonoBehaviour
|
||
10 years ago
|
{
|
||
10 years ago
|
if (property == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
10 years ago
|
List<GUIContent> objectNames = new List<GUIContent>();
|
||
10 years ago
|
|
||
|
T selectedObject = property.objectReferenceValue as T;
|
||
|
|
||
10 years ago
|
int selectedIndex = 0;
|
||
|
objectNames.Add(nullLabel);
|
||
10 years ago
|
for (int i = 0; i < objectList.Count; ++i)
|
||
10 years ago
|
{
|
||
10 years ago
|
objectNames.Add(new GUIContent(objectList[i].name));
|
||
10 years ago
|
|
||
10 years ago
|
if (selectedObject == objectList[i])
|
||
10 years ago
|
{
|
||
|
selectedIndex = i + 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
T result;
|
||
|
|
||
|
selectedIndex = EditorGUILayout.Popup(label, selectedIndex, objectNames.ToArray());
|
||
|
if (selectedIndex == 0)
|
||
|
{
|
||
|
result = null; // Null option
|
||
|
}
|
||
|
else
|
||
|
{
|
||
10 years ago
|
result = objectList[selectedIndex - 1];
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
property.objectReferenceValue = result;
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
}
|