public static bool OpenEditorWindow(string assetPath) { var guid = AssetDatabase.AssetPathToGUID(assetPath); var extension = Path.GetExtension(assetPath); if (string.IsNullOrEmpty(extension)) { return(false); } extension = extension.Substring(1).ToLowerInvariant(); if (extension != DlogGraphImporter.Extension) { return(false); } var dlogObject = DlogUtility.LoadGraphAtPath(assetPath); if (string.IsNullOrEmpty(dlogObject.AssetGuid)) { dlogObject.RecalculateAssetGuid(assetPath); DlogUtility.SaveGraph(dlogObject, false); } foreach (var activeWindow in Resources.FindObjectsOfTypeAll <DlogEditorWindow>()) { if (activeWindow.SelectedAssetGuid != guid) { continue; } // TODO: Ask user if they want to replace the current window (maybe ask to save before opening with cancel button) activeWindow.SetDlogObject(dlogObject); activeWindow.BuildWindow(); activeWindow.Focus(); return(true); } var window = EditorWindow.CreateWindow <DlogEditorWindow>(typeof(DlogEditorWindow), typeof(SceneView)); window.titleContent = EditorGUIUtility.TrTextContentWithIcon(guid, Resources.Load <Texture2D>(ResourcesUtility.IconSmall)); window.SetDlogObject(dlogObject); window.BuildWindow(); window.Focus(); return(true); }
public override void OnImportAsset(AssetImportContext ctx) { var dlogObject = DlogUtility.LoadGraphAtPath(ctx.assetPath); var icon = Resources.Load <Texture2D>(ResourcesUtility.IconBig); var runtimeIcon = Resources.Load <Texture2D>(ResourcesUtility.RuntimeIconBig); if (string.IsNullOrEmpty(dlogObject.AssetGuid) || dlogObject.AssetGuid != AssetDatabase.AssetPathToGUID(ctx.assetPath)) { dlogObject.RecalculateAssetGuid(ctx.assetPath); DlogUtility.SaveGraph(dlogObject, false); } ctx.AddObjectToAsset("MainAsset", dlogObject, icon); ctx.SetMainObject(dlogObject); var runtimeObject = ScriptableObject.CreateInstance <DlogObject>(); var filePath = ctx.assetPath; var assetNameSubStartIndex = filePath.LastIndexOf('/') + 1; var assetNameSubEndIndex = filePath.LastIndexOf('.'); var assetName = filePath.Substring(assetNameSubStartIndex, assetNameSubEndIndex - assetNameSubStartIndex); runtimeObject.name = assetName + " (Runtime)"; // Add properties runtimeObject.Properties = new List <Runtime.Property>(); runtimeObject.Properties.AddRange(dlogObject.DlogGraph.Properties.Select( property => new Runtime.Property { Type = property.Type, DisplayName = property.DisplayName, ReferenceName = property.ReferenceName, Guid = property.GUID } )); // Add nodes runtimeObject.Nodes = new List <Runtime.Node>(); foreach (var node in dlogObject.DlogGraph.Nodes) { var nodeData = JObject.Parse(node.NodeData); var runtimeNode = new Runtime.Node(); runtimeNode.Guid = node.GUID; switch (node.Type) { case "Dlog.SelfNode": runtimeNode.Type = NodeType.SELF; break; case "Dlog.NpcNode": runtimeNode.Type = NodeType.NPC; break; case "Dlog.PropertyNode": runtimeNode.Type = NodeType.PROP; runtimeNode.Temp_PropertyNodeGuid = nodeData.Value <string>("propertyGuid"); break; case "Dlog.CheckCombinerNode": runtimeNode.Type = NodeType.COMBINER; break; default: throw new NotSupportedException($"Invalid node type {node.Type}."); } // Get lines if (runtimeNode.Type == NodeType.SELF) { runtimeNode.Lines = new List <ConversationLine>(); var lines = JsonConvert.DeserializeObject <List <LineDataSelf> >(nodeData.Value <string>("lines")); foreach (var line in lines) { var runtimeLine = new ConversationLine { Message = line.Line, Next = line.PortGuidA, TriggerPort = line.PortGuidB, CheckPort = Guid.Empty.ToString() }; runtimeNode.Lines.Add(runtimeLine); } } else if (runtimeNode.Type == NodeType.NPC) { runtimeNode.Lines = new List <ConversationLine>(); var lines = JsonConvert.DeserializeObject <List <LineDataNpc> >(nodeData.Value <string>("lines")); foreach (var line in lines) { var runtimeLine = new ConversationLine { Message = line.Line, Next = line.PortGuidA, TriggerPort = line.PortGuidB, CheckPort = line.PortGuidC }; runtimeNode.Lines.Add(runtimeLine); } } runtimeObject.Nodes.Add(runtimeNode); } // Add edges runtimeObject.Edges = new List <Runtime.Edge>(); runtimeObject.Edges.AddRange(dlogObject.DlogGraph.Edges.Select( edge => new Runtime.Edge { FromNode = edge.Output, FromPort = edge.OutputPort, ToNode = edge.Input, ToPort = edge.InputPort } )); runtimeObject.BuildGraph(); ctx.AddObjectToAsset("RuntimeAsset", runtimeObject, runtimeIcon); AssetDatabase.Refresh(); EditorUtility.SetDirty(runtimeObject); }