/// <summary> /// 选择故事节点 /// </summary> /// <param name="node"></param> private void SelectStoryNode(StoryNode node) { if (node != null) { Selection.activeObject = node; } }
/// <summary> /// 清除节点下行连接 /// </summary> /// <param name="node"></param> private void ClearNodeDownChoices(StoryNode node) { if (node.Choices != null) { node.Choices.Clear(); } }
/// <summary> /// 绘制节点下按钮 /// </summary> /// <param name="node"></param> private void DrawDownButton(StoryNode node) { if (GUI.Button(new Rect(node.Rect.x + node.Rect.width / 2 - 10, node.Rect.y + node.Rect.height, 20, 20), "↓")) { m_UpStoryNode = node; CheckNodeConnect(); } }
/// <summary> /// 绘制节点上按钮 /// </summary> /// <param name="node"></param> private void DrawUpButton(StoryNode node) { if (GUI.Button(new Rect(node.Rect.x + node.Rect.width / 2 - 10, node.Rect.y - 20, 20, 20), "↑")) { m_DownStoryNode = node; CheckNodeConnect(); } }
/// <summary> /// 创建节点资源 /// </summary> /// <param name="type"></param> private void CreateStoryNodeContent(StoryNode node) { StoryContent Content = AssetCreator <StoryContent> .CreateAsset(ConfigInfo.StoryContentFolder, node.name); if (Content != null) { node.Content = Content; Selection.activeObject = Content; } RefreshStoryNodeHeight(node); }
/// <summary> /// 设置节点类型 /// </summary> /// <param name="node"></param> /// <param name="nodeType"></param> private void SetNodeType(StoryNode node, StoryNodeType nodeType) { if (node != null) { switch (nodeType) { case StoryNodeType.中间节点: if (m_CurrentStoryTree.StartNode == node) { m_CurrentStoryTree.StartNode = null; } if (m_CurrentStoryTree.EndNodes.Contains(node)) { m_CurrentStoryTree.EndNodes.Remove(node); } break; case StoryNodeType.起始节点: if (m_CurrentStoryTree.EndNodes.Contains(node)) { m_CurrentStoryTree.EndNodes.Remove(node); } if (m_CurrentStoryTree.StartNode == null) { m_CurrentStoryTree.StartNode = node; ClearNodeUpChoices(node); } else if (m_CurrentStoryTree.StartNode != node) { SetNodeType(m_CurrentStoryTree.StartNode, StoryNodeType.中间节点); m_CurrentStoryTree.StartNode = node; ClearNodeUpChoices(node); } break; case StoryNodeType.结局节点: if (m_CurrentStoryTree.StartNode == node) { m_CurrentStoryTree.StartNode = null; } m_CurrentStoryTree.EndNodes.Add(node); ClearNodeDownChoices(node); break; default: break; } node.NodeType = nodeType; } }
/// <summary> /// 创建故事节点 /// </summary> private void CreateStoryNode(StoryTree tree) { StoryNode storyNode = AssetCreator <StoryNode> .CreateAsset(ConfigInfo.StoryNodeFolder, tree.name); if (storyNode != null) { storyNode.Rect = new Rect(m_MousePos.x, m_MousePos.y, ConfigInfo.DefaultNodeSize.x, ConfigInfo.DefaultNodeSize.y); if (tree.Nodes == null) { tree.Nodes = new List <StoryNode>(); } tree.Nodes.Add(storyNode); Selection.activeObject = storyNode; } }
/// <summary> /// 刷新节点高度 /// </summary> /// <param name="node"></param> private int RefreshStoryNodeHeight(StoryNode node) { int baseHeight = 50; if (node.Content != null) { switch (node.Content.ContentType) { case StoryContentType.剧情对话: if (node.Content != null) { baseHeight = 175; } else { baseHeight = 50; } break; case StoryContentType.过场动画: if (node.Content != null) { baseHeight = 175; } else { baseHeight = 50; } break; default: break; } } int addHeight = 0; if (node.Choices != null) { addHeight = node.Choices.Count * 20; } node.Rect.height = baseHeight + addHeight; return(baseHeight); }
/// <summary> /// 删除故事节点 /// </summary> private void DeleteStoryNode(StoryNode node, bool isShow) { if (node != null) { if (isShow) { string str = "确认要删除节点 " + node.name + " 吗?"; if (EditorUtility.DisplayDialog("警告", str, "确认", "取消")) { Debug.Log("已删除节点资源:" + node.name + ".asset"); ClearNodeUpChoices(node); File.Delete(ConfigInfo.StoryNodeFolder + "/" + node.name + ".asset"); AssetDatabase.Refresh(); } } else { Debug.Log("已删除节点资源:" + node.name + ".asset"); ClearNodeUpChoices(node); File.Delete(ConfigInfo.StoryNodeFolder + "/" + node.name + ".asset"); AssetDatabase.Refresh(); } } }
/// <summary> /// 清除节点上行连接 /// </summary> /// <param name="node"></param> private void ClearNodeUpChoices(StoryNode node) { if (node.Choices != null) { foreach (StoryNode item in m_CurrentStoryTree.Nodes) { if (item == null) { continue; } if (item.Choices != null) { for (int i = 0; i < item.Choices.Count; i++) { if (item.Choices[i].NextStoryNode == node) { item.Choices.RemoveAt(i); i--; } } } } } }
public StoryChoice(string text, StoryNode nextStoryNode) { ChoiceText = text; NextStoryNode = nextStoryNode; }
private void OnEnable() { Node = (StoryNode)target; }
/// <summary> /// 绘制故事节点 /// </summary> /// <param name="storyNodes"></param> /// <returns></returns> private void DrawStoryNode(StoryNode node) { if (node != null) { //节点背景 if (m_CurrentStoryNode != null) { if (m_CurrentStoryNode == node) { EditorGUI.DrawRect(node.Rect, ConfigInfo.SelectNode); } else { EditorGUI.DrawRect(node.Rect, ConfigInfo.NormalNode); } } else { EditorGUI.DrawRect(node.Rect, ConfigInfo.NormalNode); } //节点类型 switch (node.NodeType) { case StoryNodeType.中间节点: DrawUpButton(node); DrawDownButton(node); break; case StoryNodeType.起始节点: DrawDownButton(node); break; case StoryNodeType.结局节点: DrawUpButton(node); break; default: break; } //节点属性 node.IsPassed = EditorGUI.ToggleLeft(new Rect(node.Rect.x + node.Rect.width - 45, node.Rect.y + 5, 100, 16), "通过", node.IsPassed); node.IsMainNode = EditorGUI.ToggleLeft(new Rect(node.Rect.x + node.Rect.width - 45, node.Rect.y + 25, 100, 16), "主线", node.IsMainNode); //节点内容 SerializedObject serializedObject = new SerializedObject(node); SerializedProperty Content = serializedObject.FindProperty("Content"); EditorGUI.PropertyField(new Rect(node.Rect.x + 5, node.Rect.y + 5, node.Rect.width - 65, 16), Content, GUIContent.none); //节点内容类型 if (node.Content != null) { EditorGUI.LabelField(new Rect(node.Rect.x + 5, node.Rect.y + 25, 40, 16), "类型"); node.Content.ContentType = (StoryContentType)EditorGUI.EnumPopup(new Rect(node.Rect.x + 45, node.Rect.y + 25, node.Rect.width - 123, 20), GUIContent.none, node.Content.ContentType); node.Content.IsReaded = EditorGUI.ToggleLeft(new Rect(node.Rect.x + node.Rect.width - 45, node.Rect.y + 65, 45, 16), "阅读", node.Content.IsReaded); EditorGUI.LabelField(new Rect(node.Rect.x + 5, node.Rect.y + 45, 30, 16), "周目"); node.ID.Round = EditorGUI.IntField(new Rect(node.Rect.x + 5, node.Rect.y + 65, 30, 16), node.ID.Round); EditorGUI.LabelField(new Rect(node.Rect.x + 40, node.Rect.y + 45, 30, 16), "章节"); node.ID.Chapter = EditorGUI.IntField(new Rect(node.Rect.x + 40, node.Rect.y + 65, 30, 16), node.ID.Chapter); EditorGUI.LabelField(new Rect(node.Rect.x + 75, node.Rect.y + 45, 30, 16), "场景"); node.ID.Scene = EditorGUI.IntField(new Rect(node.Rect.x + 75, node.Rect.y + 65, 30, 16), node.ID.Scene); EditorGUI.LabelField(new Rect(node.Rect.x + 110, node.Rect.y + 45, 30, 16), "片段"); node.ID.Part = EditorGUI.IntField(new Rect(node.Rect.x + 110, node.Rect.y + 65, 30, 16), node.ID.Part); EditorGUI.LabelField(new Rect(node.Rect.x + 145, node.Rect.y + 45, 30, 16), "分支"); node.ID.Branch = EditorGUI.IntField(new Rect(node.Rect.x + 145, node.Rect.y + 65, 30, 16), node.ID.Branch); EditorGUI.LabelField(new Rect(node.Rect.x + 5, node.Rect.y + 85, 40, 16), "时间"); int y = EditorGUI.IntField(new Rect(node.Rect.x + 45, node.Rect.y + 85, 40, 16), node.Content.Time.Year); int mo = EditorGUI.IntField(new Rect(node.Rect.x + 88, node.Rect.y + 85, 28, 16), node.Content.Time.Month); int d = EditorGUI.IntField(new Rect(node.Rect.x + 119, node.Rect.y + 85, 28, 16), node.Content.Time.Day); int h = EditorGUI.IntField(new Rect(node.Rect.x + 155, node.Rect.y + 85, 28, 16), node.Content.Time.Hour); int mi = EditorGUI.IntField(new Rect(node.Rect.x + 186, node.Rect.y + 85, 28, 16), node.Content.Time.Minute); int s = EditorGUI.IntField(new Rect(node.Rect.x + 217, node.Rect.y + 85, 28, 16), node.Content.Time.Second); node.Content.Time = new DateTime(y, mo, d, h, mi, s); EditorGUI.LabelField(new Rect(node.Rect.x + 5, node.Rect.y + 105, 40, 16), "地点"); node.Content.Position = EditorGUI.TextField(new Rect(node.Rect.x + 45, node.Rect.y + 105, node.Rect.width - 50, 16), node.Content.Position); EditorGUI.LabelField(new Rect(node.Rect.x + 5, node.Rect.y + 125, 40, 16), "概述"); node.Content.Summary = EditorGUI.TextArea(new Rect(node.Rect.x + 45, node.Rect.y + 125, node.Rect.width - 50, 42), node.Content.Summary); } else { EditorGUI.LabelField(new Rect(node.Rect.x + 5, node.Rect.y + 25, node.Rect.width - 83, 16), "请创建或引用一个节点内容"); } int baseHeight = RefreshStoryNodeHeight(node); //节点接点 if (node.Choices != null) { ClearStoryNodeNullChoices(node.Choices); for (int i = 0; i < node.Choices.Count; i++) { StoryNode nextNode = node.Choices[i].NextStoryNode; if (nextNode != null) { string label = ""; if (nextNode.Content != null) { label = "分支" + nextNode.ID.Branch; } else { label = "分支?"; } EditorGUI.LabelField(new Rect(node.Rect.x + 5, node.Rect.y + 20 * i + baseHeight, 40, 16), label); node.Choices[i].ChoiceText = EditorGUI.TextField(new Rect(node.Rect.x + 45, node.Rect.y + 20 * i + baseHeight, node.Rect.width - 75, 16), node.Choices[i].ChoiceText); if (GUI.Button(new Rect(node.Rect.x + node.Rect.width - 25, node.Rect.y + 20 * i + baseHeight, 20, 16), "X")) { node.Choices.RemoveAt(i); i--; } } else { node.Choices.RemoveAt(i); } } } serializedObject.ApplyModifiedProperties(); } }
/// <summary> /// 检测鼠标事件 /// </summary> private void CheckMouseEvent() { if (Event.current.type == EventType.MouseMove) { GetMousePos(); } if (Event.current.type == EventType.MouseDown) { GetMousePos(); if (m_CurrentStoryTree != null && m_CurrentStoryTree.Nodes != null) { //获取当前选中节点 bool isInNode = false; for (int i = 0; i < m_CurrentStoryTree.Nodes.Count; i++) { StoryNode node = m_CurrentStoryTree.Nodes[i]; if (node != null) { if (node.Rect.Contains(m_MousePos)) { xOffset = m_MousePos.x - node.Rect.x; yOffset = m_MousePos.y - node.Rect.y; isInNode = true; //选中此节点 m_CurrentStoryNode = node; Selection.activeObject = node; break; } } } if (!isInNode) { m_CurrentStoryNode = null; Selection.activeObject = m_CurrentStoryTree; } else { //将选中节点置顶显示 m_CurrentStoryTree.Nodes.Remove(m_CurrentStoryNode); m_CurrentStoryTree.Nodes.Insert(0, m_CurrentStoryNode); } } } if (Event.current.type == EventType.MouseDrag) { GetMousePos(); if (m_CurrentStoryNode != null) { if (m_CurrentStoryNode.Rect.Contains(m_MousePos)) { m_CurrentStoryNode.Rect = new Rect(m_MousePos.x - xOffset, m_MousePos.y - yOffset, m_CurrentStoryNode.Rect.width, m_CurrentStoryNode.Rect.height); } } } if (Event.current.type == EventType.MouseUp) { GetMousePos(); } if (Event.current.type == EventType.ContextClick) { GetMousePos(); if (m_UpStoryNode == null && m_DownStoryNode == null) { DrawContextMenu(); //设置该事件被使用 Event.current.Use(); } else { ClearTempConnect(); } } if (Event.current.isScrollWheel) { } }
/// <summary> /// 清除节点临时连接 /// </summary> private void ClearTempConnect() { m_UpStoryNode = null; m_DownStoryNode = null; }