Esempio n. 1
0
    // From: https://wiki.unity3d.com/index.php?title=CreateScriptableObjectAsset
    public static void CreateAsset(string path, List <Vector3> nodes)
    {
        GoPathSettings asset = ScriptableObject.CreateInstance <GoPathSettings>();

        asset.Nodes = nodes;

        AssetDatabase.CreateAsset(asset, AssetDatabase.GenerateUniqueAssetPath(path));

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        EditorUtility.FocusProjectWindow();
        Selection.activeObject = asset;
    }
Esempio n. 2
0
 private void saveAsScriptableObject(string path)
 {
     GoPathSettings.CreateAsset(path, _target.nodes);
 }
Esempio n. 3
0
    public override void OnInspectorGUI()
    {
        // what kind of handles shall we use?
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("Use Standard Handles");
        _target.useStandardHandles = EditorGUILayout.Toggle(_target.useStandardHandles);
        EditorGUILayout.EndHorizontal();


        // path name:
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("Route Name");
        _target.pathName = EditorGUILayout.TextField(_target.pathName);
        EditorGUILayout.EndHorizontal();

        if (_target.pathName == string.Empty)
        {
            _target.pathName = "route" + Random.Range(1, 100000);
        }


        // path color:
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("Route Color");
        _target.pathColor = EditorGUILayout.ColorField(_target.pathColor);
        EditorGUILayout.EndHorizontal();


        // force straight lines:
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("Force Straight Line Path");
        _target.forceStraightLinePath = EditorGUILayout.Toggle(_target.forceStraightLinePath);
        EditorGUILayout.EndHorizontal();


        // resolution
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("Editor Drawing Resolution");
        _target.pathResolution = EditorGUILayout.IntSlider(_target.pathResolution, 2, 100);
        EditorGUILayout.EndHorizontal();


        EditorGUILayout.Separator();


        // insert node - we need 3 or more nodes for insert to make sense
        if (_target.nodes.Count > 2)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("Insert Node");
            _insertIndex = EditorGUILayout.IntField(_insertIndex);
            if (GUILayout.Button("Insert"))
            {
                // validate the index
                if (_insertIndex >= 0 && _insertIndex < _target.nodes.Count)
                {
                    // insert the node offsetting it a bit from the previous node
                    var copyNodeIndex = _insertIndex == 0 ? 0 : _insertIndex;
                    var copyNode      = _target.nodes[copyNodeIndex];
                    copyNode.x += 10;
                    copyNode.z += 10;

                    insertNodeAtIndex(copyNode, _insertIndex);
                }
            }
            EditorGUILayout.EndHorizontal();
        }


        // close route?
        if (GUILayout.Button("Close Path"))
        {
            Undo.RecordObject(_target, "Path Vector Changed");
            closeRoute();
            GUI.changed = true;
        }


        // shift the start point to the origin
        if (GUILayout.Button("Shift Path to Start at Origin"))
        {
            Undo.RecordObject(_target, "Path Vector Changed");

            var offset = Vector3.zero;

            // see what kind of path we are. the simplest case is just a straight line
            var path = new GoSpline(_target.nodes, _target.forceStraightLinePath);
            if (path.splineType == GoSplineType.StraightLine || _target.nodes.Count < 5)
            {
                offset = Vector3.zero - _target.nodes[0];
            }
            else
            {
                offset = Vector3.zero - _target.nodes[1];
            }

            for (var i = 0; i < _target.nodes.Count; i++)
            {
                _target.nodes[i] += offset;
            }

            GUI.changed = true;
        }


        // reverse
        if (GUILayout.Button("Reverse Path"))
        {
            Undo.RecordObject(_target, "Path Vector Changed");
            _target.nodes.Reverse();
            GUI.changed = true;
        }


        // persist to disk
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Save to/Read from Disk");

        // Should we save it as a ScriptableObject instead?


        EditorGUILayout.Space();
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("Save As Scriptable Object");
        _saveAsScriptableObject = EditorGUILayout.Toggle(_saveAsScriptableObject);
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.Space();
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("Serialize and Save Path");
        if (GUILayout.Button("Save"))
        {
            if (_saveAsScriptableObject)
            {
                if (_saveAsScriptableObject)
                {
                    var folderPath = EditorUtility.OpenFolderPanel("Save Folder", Application.dataPath, "");
                    if (folderPath != string.Empty)
                    {
                        saveAsScriptableObject(folderPath.Replace(Application.dataPath, "Assets") + "/" + _target.pathName + ".asset");
                    }
                }
            }
            else
            {
                var path = EditorUtility.SaveFilePanel("Save path", Application.dataPath + "/StreamingAssets", _target.pathName + ".asset", "asset");
                if (path != string.Empty)
                {
                    persistRouteToDisk(path);

                    // fetch the filename and set it as the routeName
                    _target.pathName = Path.GetFileName(path).Replace(".asset", string.Empty);
                }
            }

            GUI.changed = true;
        }
        EditorGUILayout.EndHorizontal();


        EditorGUILayout.Space();
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("Load From Scriptable Object");
        _loadFromScriptableObject = EditorGUILayout.Toggle(_loadFromScriptableObject);
        EditorGUILayout.EndHorizontal();

        // load from disk
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("Load saved path");
        if (GUILayout.Button("Load"))
        {
            if (_loadFromScriptableObject)
            {
                var path = EditorUtility.OpenFilePanel("Choose path to load", Path.Combine(Application.dataPath, "Asset"), "asset");
                if (path != string.Empty)
                {
                    GoPathSettings asset = AssetDatabase.LoadAssetAtPath(path.Replace(Application.dataPath, "Assets"), typeof(GoPathSettings)) as GoPathSettings;
                    if (asset != null)
                    {
                        _target.nodes    = asset.Nodes;
                        _target.pathName = Path.GetFileName(path).Replace(".asset", string.Empty);
                    }
                    else
                    {
                        Debug.LogError("Incorrect file selected");
                    }
                }
            }
            else
            {
                var path = EditorUtility.OpenFilePanel("Choose path to load",
                                                       Path.Combine(Application.dataPath, "StreamingAssets"), "asset");
                if (path != string.Empty)
                {
                    if (!File.Exists(path))
                    {
                        EditorUtility.DisplayDialog("File does not exist", "Path couldn't find the file you specified",
                                                    "Close");
                    }
                    else
                    {
                        _target.nodes    = GoSpline.bytesToVector3List(File.ReadAllBytes(path));
                        _target.pathName = Path.GetFileName(path).Replace(".asset", string.Empty);
                    }
                }
                GUI.changed = true;
            }
        }
        EditorGUILayout.EndHorizontal();


        // node display
        EditorGUILayout.Space();
        _showNodeDetails = EditorGUILayout.Foldout(_showNodeDetails, "Show Node Values");
        if (_showNodeDetails)
        {
            EditorGUI.indentLevel++;
            for (int i = 0; i < _target.nodes.Count; i++)
            {
                _target.nodes[i] = EditorGUILayout.Vector3Field("Node " + (i + 1), _target.nodes[i]);
            }
            EditorGUI.indentLevel--;
        }


        // instructions
        EditorGUILayout.Space();
        EditorGUILayout.HelpBox("While dragging a node, hold down Ctrl and slowly move the cursor to snap to a nearby point\n\n" +
                                "Click the 'Close Path' button to add a new node that will close out the current path.\n\n" +
                                "Hold Command while dragging a node to snap in 5 point increments\n\n" +
                                "Double click to add a new node at the end of the path\n\n" +
                                "Hold down alt while adding a node to prepend the new node at the front of the route\n\n" +
                                "Press delete or backspace to delete the selected node\n\n" +
                                "NOTE: make sure you have the pan tool selected while editing paths", MessageType.None);


        // update and redraw:
        if (GUI.changed)
        {
            EditorUtility.SetDirty(_target);
            Repaint();
        }
    }