/// <summary> /// 根据索引获取游戏物体实体 /// </summary> /// <param name="index">索引</param> /// <returns>游戏物体实体</returns> public GameObject this[int index] { get { if (Main.m_TaskMaster) { if (index >= 0 && index < GUIDs.Count) { TaskTarget taskTarget = Main.m_TaskMaster.GetTarget(GUIDs[index]); GameObject entity = taskTarget != null ? taskTarget.gameObject : null; return(entity); } } return(null); } }
/// <summary> /// 在场景中搜索任务目标 /// </summary> private static void SearchTaskTarget(TaskGameObjects taskGameObjects, int index) { if (taskGameObjects.GUIDs[index] == "<None>") { return; } if (taskGameObjects._gameObjects[index] != null) { return; } PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage(); if (prefabStage != null) { taskGameObjects._gameObjects[index] = prefabStage.prefabContentsRoot.FindChildren(taskGameObjects.Paths[index]); if (taskGameObjects._gameObjects[index] == null) { TaskTarget[] targets = prefabStage.prefabContentsRoot.GetComponentsInChildren <TaskTarget>(true); foreach (TaskTarget target in targets) { if (taskGameObjects.GUIDs[index] == target.GUID) { taskGameObjects._gameObjects[index] = target.gameObject; taskGameObjects.Paths[index] = target.transform.FullName(); taskGameObjects.Paths[index] = taskGameObjects.Paths[index].Substring(taskGameObjects.Paths[index].IndexOf("/") + 1); break; } } } } else { taskGameObjects._gameObjects[index] = GameObject.Find(taskGameObjects.Paths[index]); if (taskGameObjects._gameObjects[index] == null) { TaskTarget[] targets = UnityEngine.Object.FindObjectsOfType <TaskTarget>(); foreach (TaskTarget target in targets) { if (taskGameObjects.GUIDs[index] == target.GUID) { taskGameObjects._gameObjects[index] = target.gameObject; taskGameObjects.Paths[index] = target.transform.FullName(); break; } } } } if (taskGameObjects._gameObjects[index] != null) { TaskTarget target = taskGameObjects._gameObjects[index].GetComponent <TaskTarget>(); if (!target) { target = taskGameObjects._gameObjects[index].AddComponent <TaskTarget>(); target.GUID = taskGameObjects.GUIDs[index]; EditorUtility.SetDirty(taskGameObjects._gameObjects[index]); } } }
protected void TaskGameObjectField(ref TaskGameObject taskGameObject, string name, float nameWidth) { if (taskGameObject == null) { taskGameObject = new TaskGameObject(); } GUILayout.BeginHorizontal(); GUIContent gUIContent = new GUIContent(name); gUIContent.tooltip = "GUID: " + taskGameObject.GUID; GUILayout.Label(gUIContent, GUILayout.Width(nameWidth)); GUI.color = taskGameObject.AgentEntity ? Color.white : Color.gray; GameObject newEntity = EditorGUILayout.ObjectField(taskGameObject.AgentEntity, typeof(GameObject), true, GUILayout.Width(Anchor.width - nameWidth - 35)) as GameObject; if (newEntity != taskGameObject.AgentEntity) { if (newEntity != null) { TaskTarget target = newEntity.GetComponent <TaskTarget>(); if (!target) { target = newEntity.AddComponent <TaskTarget>(); } if (target.GUID == "<None>") { target.GUID = Guid.NewGuid().ToString(); } taskGameObject.AgentEntity = newEntity; taskGameObject.GUID = target.GUID; taskGameObject.Path = newEntity.transform.FullName(); } } GUI.color = Color.white; if (taskGameObject.AgentEntity == null && taskGameObject.GUID != "<None>") { taskGameObject.AgentEntity = GameObject.Find(taskGameObject.Path); if (taskGameObject.AgentEntity == null) { TaskTarget[] targets = FindObjectsOfType <TaskTarget>(); foreach (TaskTarget target in targets) { if (taskGameObject.GUID == target.GUID) { taskGameObject.AgentEntity = target.gameObject; taskGameObject.Path = target.transform.FullName(); break; } } } else { TaskTarget target = taskGameObject.AgentEntity.GetComponent <TaskTarget>(); if (!target) { target = taskGameObject.AgentEntity.AddComponent <TaskTarget>(); target.GUID = taskGameObject.GUID; } } } gUIContent = EditorGUIUtility.IconContent("TreeEditor.Trash"); gUIContent.tooltip = "Delete"; GUI.enabled = taskGameObject.GUID != "<None>"; if (GUILayout.Button(gUIContent, "InvisibleButton", GUILayout.Width(20), GUILayout.Height(20))) { taskGameObject.AgentEntity = null; taskGameObject.GUID = "<None>"; taskGameObject.Path = ""; } GUI.enabled = true; GUILayout.EndHorizontal(); }
/// <summary> /// 初始化编辑器数据 /// </summary> private void InitializeEditorData(string displayName) { _displayName = displayName; if (_reorderableList == null) { _reorderableList = new ReorderableList(GUIDs, typeof(string), true, true, false, false); _reorderableList.elementHeight = 22; _reorderableList.drawHeaderCallback = (Rect rect) => { Rect sub = rect; sub.Set(rect.x, rect.y, rect.width - 60, rect.height); GUI.Label(sub, _displayName); if (!EditorApplication.isPlaying) { GUIContent gc = new GUIContent(); gc.image = EditorGUIUtility.IconContent("d_Toolbar Plus More").image; gc.tooltip = "Add a new TaskGameObject"; sub.Set(rect.x + rect.width - 40, rect.y - 2, 20, 20); if (GUI.Button(sub, gc, "InvisibleButton")) { GUIDs.Add("<None>"); Paths.Add(""); _gameObjects.Add(null); } gc.image = EditorGUIUtility.IconContent("d_Toolbar Minus").image; gc.tooltip = "Remove select TaskGameObject"; sub.Set(rect.x + rect.width - 20, rect.y - 2, 20, 20); GUI.enabled = _reorderableList.index >= 0 && _reorderableList.index < GUIDs.Count; if (GUI.Button(sub, gc, "InvisibleButton")) { GUIDs.RemoveAt(_reorderableList.index); Paths.RemoveAt(_reorderableList.index); _gameObjects.RemoveAt(_reorderableList.index); } GUI.enabled = true; } }; _reorderableList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => { if (index >= 0 && index < GUIDs.Count) { Rect sub = rect; sub.y = sub.y + 2; sub.height = 16; GUI.color = (GUIDs[index] != "<None>") ? Color.white : Color.gray; GameObject entity = EditorGUI.ObjectField(sub, _gameObjects[index], typeof(GameObject), true) as GameObject; if (entity != _gameObjects[index]) { if (entity != null) { TaskTarget target = entity.GetComponent <TaskTarget>(); if (!target) { target = entity.AddComponent <TaskTarget>(); EditorUtility.SetDirty(entity); } if (target.GUID == "<None>") { target.GUID = Guid.NewGuid().ToString(); EditorUtility.SetDirty(target); } _gameObjects[index] = entity; GUIDs[index] = target.GUID; Paths[index] = entity.transform.FullName(); } } GUI.color = Color.white; SearchTaskTarget(this, index); } }; _reorderableList.drawElementBackgroundCallback = (Rect rect, int index, bool isActive, bool isFocused) => { if (Event.current.type == EventType.Repaint) { GUIStyle gUIStyle = (index % 2 != 0) ? "CN EntryBackEven" : "CN EntryBackodd"; gUIStyle = (!isActive && !isFocused) ? gUIStyle : "RL Element"; rect.x += 2; rect.width -= 6; gUIStyle.Draw(rect, false, isActive, isActive, isFocused); } }; _reorderableList.onReorderCallbackWithDetails = (ReorderableList list, int oldIndex, int newIndex) => { string path = Paths[oldIndex]; Paths.RemoveAt(oldIndex); Paths.Insert(newIndex, path); GameObject obj = _gameObjects[oldIndex]; _gameObjects.RemoveAt(oldIndex); _gameObjects.Insert(newIndex, obj); }; } if (_gameObjects == null) { _gameObjects = new List <GameObject>(); for (int i = 0; i < GUIDs.Count; i++) { _gameObjects.Add(null); } } while (_gameObjects.Count != GUIDs.Count) { if (_gameObjects.Count < GUIDs.Count) { _gameObjects.Add(null); } else if (_gameObjects.Count > GUIDs.Count) { _gameObjects.RemoveAt(_gameObjects.Count - 1); } } }
/// <summary> /// 绘制字段 /// </summary> /// <param name="taskGameObject">目标物体</param> /// <param name="name">绘制名称</param> /// <param name="nameWidth">名称宽度</param> /// <param name="totalWidth">总宽度</param> /// <param name="copy">复制菜单的名称</param> /// <param name="paste">粘贴菜单的名称</param> public static void DrawField(TaskGameObject taskGameObject, string name, float nameWidth, float totalWidth, string copy = "Copy", string paste = "Paste") { if (taskGameObject == null) { return; } GUILayout.BeginHorizontal(); GUIContent gUIContent = new GUIContent(name); gUIContent.tooltip = "GUID: " + taskGameObject.GUID; if (GUILayout.Button(gUIContent, "Label", GUILayout.Width(nameWidth))) { GenericMenu gm = new GenericMenu(); if (taskGameObject.GUID == "<None>") { gm.AddDisabledItem(new GUIContent(copy)); } else { gm.AddItem(new GUIContent(copy), false, () => { GUIUtility.systemCopyBuffer = taskGameObject.GUID; }); } if (string.IsNullOrEmpty(GUIUtility.systemCopyBuffer)) { gm.AddDisabledItem(new GUIContent(paste)); } else { gm.AddItem(new GUIContent(paste), false, () => { taskGameObject.GUID = GUIUtility.systemCopyBuffer; }); } gm.ShowAsContext(); } GUI.color = (taskGameObject.GUID != "<None>") ? Color.white : Color.gray; GameObject newEntity = EditorGUILayout.ObjectField(taskGameObject.AgentEntity, typeof(GameObject), true, GUILayout.Width(totalWidth - nameWidth - 35)) as GameObject; if (newEntity != taskGameObject.AgentEntity) { if (newEntity != null) { TaskTarget target = newEntity.GetComponent <TaskTarget>(); if (!target) { target = newEntity.AddComponent <TaskTarget>(); EditorUtility.SetDirty(newEntity); } if (target.GUID == "<None>") { target.GUID = Guid.NewGuid().ToString(); EditorUtility.SetDirty(target); } taskGameObject.AgentEntity = newEntity; taskGameObject.GUID = target.GUID; taskGameObject.Path = newEntity.transform.FullName(); } } GUI.color = Color.white; SearchTaskTarget(taskGameObject); gUIContent.text = null; gUIContent.image = EditorGUIUtility.IconContent("TreeEditor.Trash").image; gUIContent.tooltip = "Delete"; GUI.enabled = taskGameObject.GUID != "<None>"; if (GUILayout.Button(gUIContent, "InvisibleButton", GUILayout.Width(20), GUILayout.Height(20))) { taskGameObject.AgentEntity = null; taskGameObject.GUID = "<None>"; taskGameObject.Path = ""; } GUI.enabled = true; GUILayout.EndHorizontal(); }