コード例 #1
0
        public static void BuildInspectorPropertiesElement(string elementPath, IEditorContainer editor, System.Collections.Generic.HashSet <System.Type> usedComponents, SerializedProperty obj, UnityEngine.UIElements.VisualElement container, bool noFields, System.Action <int, PropertyField> onBuild = null)
        {
            obj = obj.Copy();
            container.Clear();
            var source = obj.Copy();
            SerializedProperty iterator = obj;

            if (iterator.NextVisible(true) == false)
            {
                return;
            }
            if (iterator.NextVisible(true) == false)
            {
                return;
            }
            var depth        = iterator.depth;
            var i            = 0;
            var iteratorNext = iterator.Copy();

            do
            {
                if (string.IsNullOrEmpty(elementPath) == false)
                {
                    iterator = iteratorNext.FindPropertyRelative(elementPath);
                }
                else
                {
                    iterator = iteratorNext;
                }
                if (iterator.propertyType != SerializedPropertyType.ManagedReference)
                {
                    continue;
                }

                var element = new VisualElement();
                element.AddToClassList("element");

                var itCopy = iterator.Copy();
                GetTypeFromManagedReferenceFullTypeName(iterator.managedReferenceFullTypename, out var type);
                element.AddToClassList(i % 2 == 0 ? "even" : "odd");
                element.RegisterCallback <UnityEngine.UIElements.ContextClickEvent, int>((evt, idx) => {
                    var menu = new GenericMenu();
                    if (usedComponents != null)
                    {
                        menu.AddItem(new GUIContent("Delete"), false, () => {
                            RemoveComponent((DataConfigEditor)editor, usedComponents, source, type, noFields);
                            editor.Save();
                            BuildInspectorProperties(editor, usedComponents, source, container, noFields);
                        });

                        menu.AddItem(new GUIContent("Copy JSON"), false, () => {
                            var instance = itCopy.GetValue();
                            var json     = JsonUtility.ToJson(instance, true);
                            EditorGUIUtility.systemCopyBuffer = json;
                        });
                    }

                    editor.OnComponentMenu(menu, idx);
                    menu.ShowAsContext();
                }, i);

                if (type != null && usedComponents?.Contains(type) == false)
                {
                    usedComponents?.Add(type);
                }
                if (type == null)
                {
                    var label = new UnityEngine.UIElements.Label("MISSING: " + iterator.managedReferenceFullTypename);
                    element.name = "missing";
                    label.AddToClassList("inner-element");
                    label.AddToClassList("missing-label");
                    element.Add(label);
                }
                else if (iterator.hasVisibleChildren == false || noFields == true)
                {
                    var horizontal = new UnityEngine.UIElements.VisualElement();
                    horizontal.AddToClassList("inner-element");
                    horizontal.AddToClassList("no-fields-container");
                    element.name = type.Name;

                    var toggle = new UnityEngine.UIElements.Toggle();
                    toggle.AddToClassList("no-fields-toggle");
                    toggle.SetEnabled(false);
                    toggle.SetValueWithoutNotify(true);
                    horizontal.Add(toggle);

                    var label = new UnityEngine.UIElements.Label(GUILayoutExt.GetStringCamelCaseSpace(type.Name));
                    label.AddToClassList("no-fields-label");
                    horizontal.Add(label);

                    element.Add(horizontal);
                }
                else
                {
                    var label = GUILayoutExt.GetStringCamelCaseSpace(type.Name);
                    if (iterator.hasVisibleChildren == true)
                    {
                        var childs = iterator.Copy();
                        //var height = EditorUtilities.GetPropertyHeight(childs, true, new GUIContent(label));
                        var cnt = EditorUtilities.GetPropertyChildCount(childs);
                        if (cnt == 1 /*&& height <= 22f*/)
                        {
                            iterator.NextVisible(true);
                        }
                    }

                    var propertyField = new PropertyField(iterator.Copy(), label);
                    propertyField.BindProperty(iterator);
                    onBuild?.Invoke(i, propertyField);
                    propertyField.AddToClassList("property-field");
                    propertyField.AddToClassList("inner-element");
                    element.name = type.Name;
                    element.Add(propertyField);
                }

                if (type != null)
                {
                    var helps = type.GetCustomAttributes(typeof(ComponentHelpAttribute), false);
                    if (helps.Length > 0)
                    {
                        var label = new UnityEngine.UIElements.Label(((ComponentHelpAttribute)helps[0]).comment);
                        label.AddToClassList("comment");
                        element.Add(label);
                    }

                    if (typeof(IComponentStatic).IsAssignableFrom(type) == true)
                    {
                        var label = new UnityEngine.UIElements.Label("Static");
                        label.AddToClassList("static-component");
                        element.AddToClassList("has-static-component");
                        element.Add(label);
                    }

                    if (typeof(IComponentShared).IsAssignableFrom(type) == true)
                    {
                        var label = new UnityEngine.UIElements.Label("Shared");
                        label.AddToClassList("shared-component");
                        element.AddToClassList("has-shared-component");
                        element.Add(label);
                    }
                }

                container.Add(element);
                ++i;
            } while (iteratorNext.NextVisible(false) == true && depth <= iteratorNext.depth);
        }
コード例 #2
0
        internal static bool Create(string path, string fileName, string templateName, System.Collections.Generic.Dictionary <string, string> customDefines = null, bool allowRename = true, System.Action <Object> onCreated = null)
        {
            var templateAsset = EditorUtilities.Load <TextAsset>($"Editor/Templates/EditorResources/{templateName}.txt", true);
            var content       = templateAsset.text;

            if (customDefines != null)
            {
                foreach (var def in customDefines)
                {
                    content = content.Replace("#" + def.Key + "#", def.Value);
                }
            }

            var stateTypeStr = "StateClassType";

            if (content.Contains("#STATENAME#") == true)
            {
                var projectName = path.Split('/');
                var type        = typeof(ME.ECS.State);
                var types       = System.AppDomain.CurrentDomain.GetAssemblies().SelectMany(s => s.GetTypes())
                                  .Where(p => p.IsClass == true && type.IsAssignableFrom(p) && projectName.Contains(p.Name.Replace("State", string.Empty))).ToArray();
                if (types.Length > 0)
                {
                    var stateType = types[0];
                    stateTypeStr = stateType.Name;
                }
            }

            var @namespace = path.Replace("Assets/", "").Replace("/", ".").Replace("\\", ".");

            content = content.Replace(@"#NAMESPACE#", @namespace);
            content = content.Replace(@"#PROJECTNAME#", @namespace.Split('.')[0]);
            content = content.Replace(@"#STATENAME#", stateTypeStr);
            content = content.Replace(@"#REFERENCES#", string.Empty);

            if (allowRename == true)
            {
                var defaultNewFileName = fileName;
                var image = ScriptTemplates.scriptIcon;
                ProjectWindowUtil.StartNameEditingIfProjectWindowExists(
                    0,
                    ScriptableObject.CreateInstance <DoCreateScriptAsset>().SetCallback((instance) => {
                    if (onCreated != null)
                    {
                        onCreated.Invoke(instance);
                    }
                }),
                    defaultNewFileName,
                    image,
                    content);
            }
            else
            {
                var fullDir = path + "/" + fileName;
                if (System.IO.File.Exists(fullDir) == true)
                {
                    var contentExists = System.IO.File.ReadAllText(fullDir);
                    if (contentExists == content)
                    {
                        return(false);
                    }
                }

                var withoutExtension = System.IO.Path.GetFileNameWithoutExtension(fullDir);
                withoutExtension = withoutExtension.Replace(" ", "");
                content          = content.Replace("#SCRIPTNAME#", withoutExtension);

                var dir = System.IO.Path.GetDirectoryName(fullDir);
                if (System.IO.Directory.Exists(dir) == false)
                {
                    return(false);
                }

                System.IO.File.WriteAllText(fullDir, content);
                AssetDatabase.ImportAsset(fullDir, ImportAssetOptions.ForceSynchronousImport);

                if (onCreated != null)
                {
                    onCreated.Invoke(AssetDatabase.LoadAssetAtPath <Object>(fullDir));
                }
            }

            return(true);
        }
コード例 #3
0
        public static void GenerateFilters()
        {
            const int count = 20;

            var asms = UnityEditor.AssetDatabase.FindAssets("t:asmdef ME.ECS");

            foreach (var asm in asms)
            {
                var asset = UnityEditor.AssetDatabase.GUIDToAssetPath(asm);
                var dir   = $"{System.IO.Path.GetDirectoryName(asset)}/Core/Filters/CodeGenerator";
                if (System.IO.Directory.Exists(dir) == false)
                {
                    continue;
                }

                var outputDelegates = string.Empty;
                var outputForEach   = string.Empty;
                var buffers         = string.Empty;

                for (int j = 1; j < count; ++j)
                {
                    var itemsType = "T0";
                    for (int i = 1; i < j; ++i)
                    {
                        itemsType += $",T{i}";
                    }

                    var itemsWhere = " where T0:unmanaged,IComponentBase";
                    for (int i = 1; i < j; ++i)
                    {
                        itemsWhere += $" where T{i}:unmanaged,IComponentBase";
                    }

                    {
                        var resForEachSource = EditorUtilities.Load <UnityEngine.TextAsset>("Editor/Templates/EditorResources/00-FilterExtensionsForEachItem.txt", isRequired: true).text;

                        var resSource  = EditorUtilities.Load <UnityEngine.TextAsset>("Editor/Templates/EditorResources/00-FilterExtensionsDelegateItem.txt", isRequired: true).text;
                        var formsWrite = string.Empty;
                        for (int i = 0; i < j; ++i)
                        {
                            formsWrite += "W";
                        }

                        var items = new List <string>();
                        items.Add("ref T0 t0");
                        for (int i = 1; i < j; ++i)
                        {
                            items.Add($"ref T{i} t{i}");
                        }

                        var itemsGet = new List <string>();
                        itemsGet.Add("ref bag.GetT0(i)");
                        for (int i = 1; i < j; ++i)
                        {
                            itemsGet.Add($"ref bag.GetT{i}(i)");
                        }

                        for (int i = 0; i <= j; ++i)
                        {
                            var chars = formsWrite.ToCharArray();

                            var res = resSource;
                            res              = res.Replace("#ITEMS_TYPE#", itemsType);
                            res              = res.Replace("#ITEMS#", string.Join(",", items));
                            res              = res.Replace("#INDEX#", j.ToString());
                            res              = res.Replace("#FORMS#", formsWrite);
                            outputDelegates += $"{res}\n";

                            var resForEach = resForEachSource;
                            resForEach     = resForEach.Replace("#ITEMS_TYPE#", itemsType);
                            resForEach     = resForEach.Replace("#ITEMS_WHERE#", itemsWhere);
                            resForEach     = resForEach.Replace("#FORMS#", formsWrite);
                            resForEach     = resForEach.Replace("#ITEMS_GET#", string.Join(",", itemsGet));
                            resForEach     = resForEach.Replace("#INDEX#", j.ToString());
                            outputForEach += $"{resForEach}\n";

                            if (i == j)
                            {
                                break;
                            }

                            itemsGet[i] = $"in bag.GetT{i}(i)";
                            items[i]    = $"in T{i} t{i}";
                            chars[i]    = 'R';
                            formsWrite  = new string(chars);
                        }
                    }

                    {
                        var itemsGet = "ref buffer.GetT0(id)";
                        for (int i = 1; i < j; ++i)
                        {
                            itemsGet += $", ref buffer.GetT{i}(id)";
                        }

                        var res = EditorUtilities.Load <UnityEngine.TextAsset>("Editor/Templates/EditorResources/00-FilterExtensionsForEachItem.txt", isRequired: true).text;
                        res = res.Replace("#ITEMS_TYPE#", itemsType);
                        res = res.Replace("#ITEMS_WHERE#", itemsWhere);
                        res = res.Replace("#ITEMS_GET#", itemsGet);
                        res = res.Replace("#INDEX#", j.ToString());
                        //outputForEach += $"{res}\n";
                    }

                    {
                        var itemMethods  = EditorUtilities.Load <UnityEngine.TextAsset>("Editor/Templates/EditorResources/00-FilterExtensionsBufferMethods.txt", isRequired: true).text;
                        var itemsMethods = string.Empty;
                        for (int i = 0; i < j; ++i)
                        {
                            var text = itemMethods;
                            text          = text.Replace("#INDEX#", i.ToString());
                            itemsMethods += text;
                        }

                        var dataBufferContains       = EditorUtilities.Load <UnityEngine.TextAsset>("Editor/Templates/EditorResources/00-FilterExtensionsBufferDataBufferContains.txt", isRequired: true).text;
                        var dataBufferContainsOutput = string.Empty;
                        for (int i = 0; i < j; ++i)
                        {
                            var text = dataBufferContains;
                            text = text.Replace("#INDEX#", i.ToString());
                            dataBufferContainsOutput += text;
                        }

                        var dataBufferOps       = EditorUtilities.Load <UnityEngine.TextAsset>("Editor/Templates/EditorResources/00-FilterExtensionsBufferDataBufferOps.txt", isRequired: true).text;
                        var dataBufferOpsOutput = string.Empty;
                        for (int i = 0; i < j; ++i)
                        {
                            var text = dataBufferOps;
                            text = text.Replace("#INDEX#", i.ToString());
                            dataBufferOpsOutput += text;
                        }

                        var dataBufferData       = EditorUtilities.Load <UnityEngine.TextAsset>("Editor/Templates/EditorResources/00-FilterExtensionsBufferDataBufferData.txt", isRequired: true).text;
                        var dataBufferDataOutput = string.Empty;
                        for (int i = 0; i < j; ++i)
                        {
                            var text = dataBufferData;
                            text = text.Replace("#INDEX#", i.ToString());
                            dataBufferDataOutput += text;
                        }

                        var regsInit       = EditorUtilities.Load <UnityEngine.TextAsset>("Editor/Templates/EditorResources/00-FilterExtensionsBufferRegsInit.txt", isRequired: true).text;
                        var regsInitOutput = string.Empty;
                        for (int i = 0; i < j; ++i)
                        {
                            var text = regsInit;
                            text            = text.Replace("#INDEX#", i.ToString());
                            regsInitOutput += text;
                        }

                        var regsInitFill       = EditorUtilities.Load <UnityEngine.TextAsset>("Editor/Templates/EditorResources/00-FilterExtensionsBufferRegsInitFill.txt", isRequired: true).text;
                        var regsInitFillOutput = string.Empty;
                        for (int i = 0; i < j; ++i)
                        {
                            var text = regsInitFill;
                            text = text.Replace("#INDEX#", i.ToString());
                            regsInitFillOutput += text;
                        }

                        var pushRegsInit       = EditorUtilities.Load <UnityEngine.TextAsset>("Editor/Templates/EditorResources/00-FilterExtensionsBufferPushRegsInit.txt", isRequired: true).text;
                        var pushRegsInitOutput = string.Empty;
                        for (int i = 0; i < j; ++i)
                        {
                            var text = pushRegsInit;
                            text = text.Replace("#INDEX#", i.ToString());
                            pushRegsInitOutput += text;
                        }

                        var pushOps       = EditorUtilities.Load <UnityEngine.TextAsset>("Editor/Templates/EditorResources/00-FilterExtensionsBufferPushOps.txt", isRequired: true).text;
                        var pushOpsOutput = string.Empty;
                        for (int i = 0; i < j; ++i)
                        {
                            var text = pushOps;
                            text           = text.Replace("#INDEX#", i.ToString());
                            pushOpsOutput += text;
                        }

                        var regsDispose       = EditorUtilities.Load <UnityEngine.TextAsset>("Editor/Templates/EditorResources/00-FilterExtensionsBufferRegsDispose.txt", isRequired: true).text;
                        var regsDisposeOutput = string.Empty;
                        for (int i = 0; i < j; ++i)
                        {
                            var text = regsDispose;
                            text = text.Replace("#INDEX#", i.ToString());
                            regsDisposeOutput += text;
                        }

                        var regsJobInit       = EditorUtilities.Load <UnityEngine.TextAsset>("Editor/Templates/EditorResources/00-FilterExtensionsBufferRegsJobInit.txt", isRequired: true).text;
                        var regsJobInitOutput = string.Empty;
                        for (int i = 0; i < j; ++i)
                        {
                            var text = regsJobInit;
                            text = text.Replace("#INDEX#", i.ToString());
                            regsJobInitOutput += text;
                        }

                        var regsJobFill       = EditorUtilities.Load <UnityEngine.TextAsset>("Editor/Templates/EditorResources/00-FilterExtensionsBufferRegsJobFill.txt", isRequired: true).text;
                        var regsJobFillOutput = string.Empty;
                        for (int i = 0; i < j; ++i)
                        {
                            var text = regsJobFill;
                            text = text.Replace("#INDEX#", i.ToString());
                            regsJobFillOutput += text;
                        }

                        var res = EditorUtilities.Load <UnityEngine.TextAsset>("Editor/Templates/EditorResources/00-FilterExtensionsBufferItem.txt", isRequired: true).text;
                        res = res.Replace("#ITEMS_TYPE#", itemsType);
                        res = res.Replace("#ITEMS_WHERE#", itemsWhere);
                        res = res.Replace("#ITEMS_METHODS#", itemsMethods);

                        res = res.Replace("#DATABUFFER_CONTAINS#", dataBufferContainsOutput);
                        res = res.Replace("#DATABUFFER_OPS#", dataBufferOpsOutput);
                        res = res.Replace("#DATABUFFER_DATA#", dataBufferDataOutput);
                        res = res.Replace("#REGS_INIT#", regsInitOutput);
                        res = res.Replace("#REGS_FILL#", regsInitFillOutput);
                        res = res.Replace("#PUSH_REGS_INIT#", pushRegsInitOutput);
                        res = res.Replace("#PUSH_OPS#", pushOpsOutput);

                        res = res.Replace("#REGS_DISPOSE#", regsDisposeOutput);
                        res = res.Replace("#JOB_INIT_ITEMS#", regsJobInitOutput);
                        res = res.Replace("#JOB_FILL_ITEMS#", regsJobFillOutput);

                        res      = res.Replace("#INDEX#", j.ToString());
                        buffers += $"{res}\n";
                    }
                }

                if (string.IsNullOrEmpty(outputDelegates) == false)
                {
                    ME.ECSEditor.ScriptTemplates.Create(dir, "Filters.Delegates.gen.cs", "00-FilterExtensionsDelegates", new Dictionary <string, string>()
                    {
                        { "CONTENT", outputDelegates }
                    }, allowRename: false);
                }
                if (string.IsNullOrEmpty(outputForEach) == false)
                {
                    ME.ECSEditor.ScriptTemplates.Create(dir, "Filters.ForEach.gen.cs", "00-FilterExtensionsForEach", new Dictionary <string, string>()
                    {
                        { "CONTENT", outputForEach }, { "CONTENT_BUFFERS", buffers }
                    }, allowRename: false);
                }
            }
        }