예제 #1
0
        static void CreateParticle()
        {
            if (Array.TrueForAll(Selection.objects, selected => !(selected is Texture)))
            {
                Debug.LogError("No textures were selected.");
                return;
            }

            for (int i = 0; i < Selection.objects.Length; i++)
            {
                Texture texture = Selection.objects[i] as Texture;

                if (texture == null)
                {
                    continue;
                }

                string textureName  = texture.name.EndsWith("Texture") ? texture.name.Substring(0, texture.name.Length - "Texture".Length) : texture.name;
                string texturePath  = AssetDatabase.GetAssetPath(texture);
                string materialPath = Path.GetDirectoryName(texturePath) + "/" + textureName + ".mat";

                AssetDatabase.CopyAsset(AssetDatabaseUtility.GetAssetPath("GraphicsTools/ParticleMaterial.mat"), materialPath);
                AssetDatabase.Refresh();

                Material material = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material;
                material.mainTexture = texture;
            }
        }
예제 #2
0
        static void CreateSprite()
        {
            if (Array.TrueForAll(Selection.objects, selected => !(selected is Texture)))
            {
                Debug.LogError("No sprites were selected.");
                return;
            }

            for (int i = 0; i < Selection.objects.Length; i++)
            {
                Texture texture = Selection.objects[i] as Texture;

                if (texture == null)
                {
                    continue;
                }

                string textureName  = texture.name.EndsWith("Texture") ? texture.name.Substring(0, texture.name.Length - "Texture".Length) : texture.name;
                string texturePath  = AssetDatabase.GetAssetPath(texture);
                string materialPath = Path.GetDirectoryName(texturePath) + "/" + textureName + ".mat";

                Sprite sprite = AssetDatabase.LoadAssetAtPath(texturePath, typeof(Sprite)) as Sprite;

                if (sprite == null)
                {
                    Debug.LogError(string.Format("Texture {0} must be imported as a sprite.", texture.name));
                    continue;
                }

                AssetDatabase.CopyAsset(AssetDatabaseUtility.GetAssetPath("GraphicsTools/SpriteMaterial.mat"), materialPath);
                AssetDatabase.Refresh();

                Material material = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material;

                GameObject     gameObject     = new GameObject(textureName);
                GameObject     child          = gameObject.AddChild("Sprite");
                SpriteRenderer spriteRenderer = child.AddComponent <SpriteRenderer>();

                spriteRenderer.sprite   = sprite;
                spriteRenderer.material = material;

                PrefabUtility.CreatePrefab(Path.GetDirectoryName(texturePath) + "/" + textureName + ".prefab", gameObject);
                AssetDatabase.Refresh();

                gameObject.Destroy();
            }
        }
예제 #3
0
        void GenerateStates()
        {
#if !UNITY_WEBPLAYER
            for (int i = 0; i < states.Count; i++)
            {
                string        state         = states[i];
                string        stateFileName = Layer.Capitalized() + state.Capitalized() + ".cs";
                string        stateInherit  = "PState";
                List <string> script        = new List <string>();

                if (string.IsNullOrEmpty(state))
                {
                    continue;
                }

                if (!lockedStates.Contains(state) && !string.IsNullOrEmpty(AssetDatabaseUtility.GetAssetPath(stateFileName)))
                {
                    Debug.LogError(string.Format("A script named {0} already exists.", stateFileName));
                    continue;
                }

                if (StateMachineUtility.LayerFormattedStateFormattedDict.ContainsKey(Inherit) && StateMachineUtility.LayerFormattedStateFormattedDict[Inherit].Contains(state))
                {
                    stateInherit = StateMachineUtility.LayerFormattedTypeDict[Inherit].GetName() + state;
                }

                script.Add("using UnityEngine;");
                script.Add("using System;");
                script.Add("using System.Collections;");
                script.Add("using System.Collections.Generic;");
                script.Add("using System.Linq;");
                script.Add("using Pseudo;");
                script.Add("");
                script.Add("public class " + Layer + state + " : " + stateInherit);
                script.Add("{");
                AddMachine(script, Machine);
                AddLayer(script, Layer);
                script.Add("	");
                AddCallbacks(script, CallbackMask);
                script.Add("	");
                script.Add("}");

                File.WriteAllLines(assetsPath + Path + System.IO.Path.AltDirectorySeparatorChar + stateFileName, script.ToArray());
            }
#endif
        }
예제 #4
0
        void GenerateLayer()
        {
#if !UNITY_WEBPLAYER
            string        layerFileName = Layer.Capitalized() + ".cs";
            string        layerInherit  = "PStateLayer";
            string        layerSublayer = "";
            List <string> script        = new List <string>();

            if (!string.IsNullOrEmpty(AssetDatabaseUtility.GetAssetPath(layerFileName)))
            {
                return;
            }

            if (StateMachineUtility.LayerFormattedTypeDict.ContainsKey(Inherit))
            {
                layerInherit = StateMachineUtility.LayerFormattedTypeDict[Inherit].GetName();
            }

            if (StateMachineUtility.LayerFormattedTypeDict.ContainsKey(SubLayer))
            {
                layerSublayer = StateMachineUtility.LayerFormattedTypeDict[SubLayer].GetName();
            }

            script.Add("using UnityEngine;");
            script.Add("using System.Collections;");
            script.Add("using System.Collections.Generic;");
            script.Add("using System.Linq;");
            script.Add("using Pseudo;");
            script.Add("");
            script.Add("public class " + Layer + " : " + layerInherit + " {");
            script.Add("	");
            AddMachine(script, Machine);
            AddLayer(script, layerSublayer);
            AddCallbacks(script, CallbackMask);
            script.Add("}");

            File.WriteAllLines(assetsPath + System.IO.Path.AltDirectorySeparatorChar + Path + System.IO.Path.AltDirectorySeparatorChar + layerFileName, script.ToArray());
#endif
        }
예제 #5
0
        void ShowLayerOptions(StateLayer layer, Rect rect)
        {
            bool isScrolling = (Screen.width - rect.xMax) > 5;

            GUIStyle style = new GUIStyle("MiniToolbarPopup");

            style.fontStyle = FontStyle.Bold;

            rect.x     = Screen.width - (isScrolling ? 60 : 45) - EditorGUI.indentLevel * 15;
            rect.y    += 1;
            rect.width = 24 + EditorGUI.indentLevel * 15;

            if (!Application.isPlaying && (Event.current.type == EventType.ExecuteCommand || rect.Contains(Event.current.mousePosition)))
            {
                List <UnityEngine.Object> validParents = new List <UnityEngine.Object>();
                List <Type>   layerTypes = new List <Type>();
                List <string> options    = new List <string> {
                    "..."
                };
                bool machineIsParent = StateMachineUtility.IsParent(layer, machine);

                foreach (KeyValuePair <Type, List <Type> > pair in StateMachineUtility.LayerTypeStateTypeDict)
                {
                    PropertyInfo layerProperty   = pair.Key.GetProperty("Layer", ReflectionExtensions.AllFlags);
                    PropertyInfo machineProperty = pair.Key.GetProperty("Machine", ReflectionExtensions.AllFlags);

                    if ((machineProperty == null || machineProperty.PropertyType.IsInstanceOfType(machine)) && (layerProperty == null || layerProperty.PropertyType.IsInstanceOfType(layer)) && Array.TrueForAll(existingLayers, existingLayer => existingLayer.GetType() != pair.Key))
                    {
                        layerTypes.Add(pair.Key);
                        options.Add("Add/" + StateMachineUtility.LayerTypeFormattedDict[pair.Key]);
                    }
                }

                if (!machineIsParent)
                {
                    validParents.Add(machine);
                    options.Add("Move To/Machine");
                }

                for (int i = 0; i < existingLayers.Length; i++)
                {
                    StateLayer existingLayer = existingLayers[i];

                    if (layer != existingLayer && !StateMachineUtility.IsParent(layer, existingLayer) && !StateMachineUtility.GetSubLayersRecursive(layer).Contains(existingLayer))
                    {
                        validParents.Add(existingLayer);
                        options.Add("Move To/" + StateMachineUtility.FormatLayer(existingLayer.GetType()));
                    }
                }

                options.Add("Copy");

                if (EditorPrefs.GetString("Clipboard Layer Type") == layer.GetType().Name)
                {
                    options.Add("Paste/Layer");
                    options.Add("Paste/Layer and States");
                    options.Add("Paste/Layer and Sublayers");
                    options.Add("Paste/All");
                }

                options.Add("Generate");

                if (Selection.gameObjects.Length <= 1)
                {
                    //					int index = EditorGUI.Popup(rect, layerTypes.IndexOf(selectedLayerType) + 1, options.ToArray(), style) - 1;
                    int    index  = EditorGUI.Popup(rect, 0, options.ToArray(), style) - 1;
                    string option = index == -1 ? "" : options[index + 1];

                    if (index < layerTypes.Count)
                    {
                        selectedLayerType = index == -1 ? null : layerTypes[Mathf.Clamp(index, 0, options.Count - 1)];

                        if (selectedLayerType != null)
                        {
                            StateMachineUtility.AddLayer(machine, selectedLayerType, layer);
                            selectedLayerType = null;
                        }
                    }
                    else if (option.StartsWith("Move To"))
                    {
                        UnityEngine.Object parent = validParents[index - layerTypes.Count];
                        StateMachineUtility.MoveLayerTo(layer, parent);
                    }
                    else if (option.StartsWith("Copy"))
                    {
                        EditorPrefs.SetString("Clipboard Layer Type", layer.GetType().Name);
                        EditorPrefs.SetInt("Clipboard Layer ID", layer.GetInstanceID());
                    }
                    else if (option.StartsWith("Paste"))
                    {
                        StateLayer layerToCopy = EditorUtility.InstanceIDToObject(EditorPrefs.GetInt("Clipboard Layer ID")) as StateLayer;

                        if (option == "Paste/Layer")
                        {
                            StateMachineUtility.CopyLayer(layer, layerToCopy, false, false);
                        }
                        else if (option == "Paste/Layer and States")
                        {
                            StateMachineUtility.CopyLayer(layer, layerToCopy, true, false);
                        }
                        else if (option == "Paste/Layer and Sublayers")
                        {
                            StateMachineUtility.CopyLayer(layer, layerToCopy, false, true);
                        }
                        else if (option == "Paste/All")
                        {
                            StateMachineUtility.CopyLayer(layer, layerToCopy, true, true);
                        }

                        EditorPrefs.SetString("Clipboard Layer Type", "");
                        EditorPrefs.SetInt("Clipboard Layer ID", 0);
                    }
                    else if (option.StartsWith("Generate"))
                    {
                        StateMachineGeneratorWindow generator = StateMachineGeneratorWindow.Create();

                        string layerTypeName   = layer.GetTypeName();
                        string layerScriptPath = AssetDatabaseUtility.GetAssetPath(layerTypeName + ".cs");

                        generator.Path     = string.IsNullOrEmpty(layerScriptPath) ? generator.Path : Path.GetDirectoryName(layerScriptPath);
                        generator.Machine  = StateMachineUtility.FormatMachine(machine);
                        generator.Layer    = layerTypeName;
                        generator.Inherit  = StateMachineUtility.FormatLayer(layer.GetType().BaseType);
                        generator.SubLayer = layer.Layer == null ? "" : StateMachineUtility.FormatLayer(layer.Layer.GetType());
                    }
                }
            }
            else
            {
                EditorGUI.BeginDisabledGroup(Application.isPlaying);

                EditorGUI.Popup(rect, 0, new[] { "..." }, style);

                EditorGUI.EndDisabledGroup();
            }
        }