示例#1
0
            internal object TryConvert()
            {
                switch (type)
                {
                case ExportedType.Bool:
                    bool.TryParse(data, out var resultBool);
                    return(resultBool);

                case ExportedType.Number:
                    double.TryParse(data, out var resultDouble);
                    return(resultDouble);

                case ExportedType.String:
                    return(data);

                case ExportedType.ObjectReference:
                    var parts = data.Split(';');
                    if (parts.Length != 2)
                    {
                        return(null);
                    }
                    if (GlobalObjectId.TryParse(parts[0], out var id))
                    {
                        return(new ObjectValue(GlobalObjectId.GlobalObjectIdentifierToObjectSlow(id), parts[1]));
                    }
                    return(data);

                case ExportedType.Color:
                    ColorUtility.TryParseHtmlString(data, out var resultColor);
                    return(resultColor);
                }
                return(null);
            }
示例#2
0
    protected override void OnUpdate()
    {
        List <Bridge.SelectorMatchRecord> l = new List <Bridge.SelectorMatchRecord>();

        Bridge.StyleMatchingContext ctx = new Bridge.StyleMatchingContext(null);
        //(element, info) => Debug.Log($"Matches: {element}"));
        Assert.IsTrue(GlobalObjectId.TryParse("GlobalObjectId_V1-1-e6e2a2a07016443409378c352d64f325-7433441132597879392-0", out var ssgoid));
        ctx.styleSheetStack = new List <StyleSheet> {
            (StyleSheet)GlobalObjectId.GlobalObjectIdentifierToObjectSlow(ssgoid)
        };

        var backgrounds = GetComponentDataFromEntity <Background>();

        Entities.ForEach((Entity e, ref UiRenderBounds bounds) =>
        {
            _eachEntity.Begin();
            l.Clear();
            ctx.currentElement = new UiEntity(e, PostUpdateCommands, backgrounds);
            _matchMarker.Begin();
            Bridge.StyleSelectorHelper.FindMatches(ctx, l);
            _matchMarker.End();
            if (l.Count > 0)
            {
//                string Rules(StyleRule info) => String.Join(",", info.properties.Select(p => $"{p.name}: {p.values}"));
//                Debug.Log(String.Join("\r\n", l.Select(x => $"{x.complexSelector} {Rules(x.complexSelector.rule)}")));
                _applyMatching.Begin();
                foreach (var record in l)
                {
                    Apply(ctx, record);
                }
                _applyMatching.End();
            }
            _eachEntity.End();
        });
    }
示例#3
0
        private static Object ToObject(SearchItem item, Type type)
        {
            if (!GlobalObjectId.TryParse(item.id, out var gid))
            {
                return(null);
            }

            var obj = GlobalObjectId.GlobalObjectIdentifierToObjectSlow(gid);

            if (obj)
            {
                if (type == null)
                {
                    return(obj);
                }
                var objType = obj.GetType();
                if (objType == type || objType.IsSubclassOf(type))
                {
                    return(obj);
                }

                if (obj is GameObject go)
                {
                    return(go.GetComponent(type));
                }
            }

            var assetPath = AssetDatabase.GUIDToAssetPath(gid.assetGUID.ToString());

            return(AssetDatabase.LoadMainAssetAtPath(assetPath));
        }
示例#4
0
        internal static IBuildStep Deserialize(string json)
        {
            if (string.IsNullOrEmpty(json))
            {
                return(null);
            }

            if (GlobalObjectId.TryParse(json, out var id))
            {
                if (GlobalObjectId.GlobalObjectIdentifierToObjectSlow(id) is BuildPipeline pipeline)
                {
                    return(pipeline);
                }
            }
            else
            {
                var type = Type.GetType(json);
                if (TypeConstruction.TryConstruct <IBuildStep>(type, out var step))
                {
                    return(step);
                }
            }

            return(null);
        }
        public object Deserialize(SerializedValueView view)
        {
            var json = view.ToString();

            if (string.IsNullOrEmpty(json))
            {
                return(null);
            }

            if (GlobalObjectId.TryParse(json, out var id))
            {
                if (GlobalObjectId.GlobalObjectIdentifierToObjectSlow(id) is BuildPipeline pipeline)
                {
                    return(pipeline);
                }
            }
            else
            {
                if (TypeConstructionHelper.TryConstructFromAssemblyQualifiedTypeName <IBuildStep>(json, out var step))
                {
                    return(step);
                }
            }
            return(null);
        }
示例#6
0
 protected override void OnCreate()
 {
     mesh          = CreateQuad();
     m_RenderQuery = GetEntityQuery(
         ComponentType.ReadOnly <UiRenderBounds>()
         );
     GlobalObjectId.TryParse(UIMaterialGOID, out var goid);
     material         = (Material)GlobalObjectId.GlobalObjectIdentifierToObjectSlow(goid);
     _colorPropertyId = Shader.PropertyToID("_Color");
     _gen             = new TextGenerator();
 }
示例#7
0
        private Object GetContextObject(ref RunItemContext ctx)
        {
            var item = ctx.item;
            var guid = guids[item.index];

            if (GlobalObjectId.TryParse(guid, out var globalId))
            {
                // Try loading the object
                // TODO: Upcoming changes to GlobalObjectIdentifierToObjectSlow will allow it
                //       to return direct references to prefabs and their children.
                //       Once that change happens there are several items which should be adjusted.
                var obj = GlobalObjectId.GlobalObjectIdentifierToObjectSlow(globalId);

                // If the object was not loaded, it is probably part of an unopened scene;
                // if so, then the solution is to first load the scene here.
                var objIsInSceneOrPrefab = globalId.identifierType == 2; // 2 is IdentifierType.kSceneObject
                if (!obj &&
                    objIsInSceneOrPrefab)
                {
                    // Open the Containing Scene Asset in the Hierarchy so the Object can be manipulated
                    var mainAssetPath = AssetDatabase.GUIDToAssetPath(globalId.assetGUID);
                    var mainAsset     = AssetDatabase.LoadAssetAtPath <Object>(mainAssetPath);
                    AssetDatabase.OpenAsset(mainAsset);

                    // If a prefab stage was opened, then mainAsset is the root of the
                    // prefab that contains the target object, so reference that for now,
                    // until GlobalObjectIdentifierToObjectSlow is updated
                    if (PrefabStageUtility.GetCurrentPrefabStage() != null)
                    {
                        obj = mainAsset;
                    }

                    // Reload object if it is still null (because it's in a previously unopened scene)
                    if (!obj)
                    {
                        obj = GlobalObjectId.GlobalObjectIdentifierToObjectSlow(globalId);
                        if (!obj)
                        {
                            ctx.didFail = true;
                            ctx.info    = $"Object {globalId.assetGUID} failed to load...";
                        }
                    }
                }

                return(obj);
            }

            ctx.didFail = true;
            ctx.info    = $"Failed to parse Global ID {item.descriptor.info}...";

            return(null);
        }
示例#8
0
        private static Object ToObject(SearchItem item, Type type)
        {
            if (!GlobalObjectId.TryParse(item.id, out var gid))
            {
                return(null);
            }

            var assetPath = AssetDatabase.GUIDToAssetPath(gid.assetGUID.ToString());

            return(ToObjectType(GlobalObjectId.GlobalObjectIdentifierToObjectSlow(gid), type) ??
                   ToObjectType(AssetDatabase.LoadAssetAtPath(assetPath, type), type) ??
                   ToObjectType(AssetDatabase.LoadMainAssetAtPath(assetPath), type));
        }
示例#9
0
        private static bool SelectObjectbyId(string id, out string guid)
        {
            guid = null;
            if (!GlobalObjectId.TryParse(id, out var gid))
            {
                return(false);
            }
            guid = gid.assetGUID.ToString();
            var obj = GlobalObjectId.GlobalObjectIdentifierToObjectSlow(gid);

            if (obj)
            {
                Utils.SelectObject(obj);
                return(true);
            }
            return(false);
        }
        private Object LoadObject(ref RunItemContext ctx)
        {
            var item = ctx.item;
            var guid = guids[item.index];

            if (GlobalObjectId.TryParse(guid, out var gid))
            {
                var obj = GlobalObjectId.GlobalObjectIdentifierToObjectSlow(gid);
                if (!obj)
                {
                    // Open container scene
                    if (gid.identifierType == (int)IdentifierType.kSceneObject)
                    {
                        var containerPath = AssetDatabase.GUIDToAssetPath(gid.assetGUID);

                        var mainInstanceID = AssetDatabase.LoadAssetAtPath <Object>(containerPath);
                        AssetDatabase.OpenAsset(mainInstanceID);

                        // if we have a prefab open, then we already have the object we need to update
                        var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
                        if (prefabStage != null)
                        {
                            obj = mainInstanceID;
                        }

                        // Reload object if it is still null
                        if (obj == null)
                        {
                            obj = GlobalObjectId.GlobalObjectIdentifierToObjectSlow(gid);
                            if (!obj)
                            {
                                ctx.didFail = true;
                                ctx.info    = $"Object {gid.assetGUID} failed to load...";
                            }
                        }
                    }
                }

                return(obj);
            }

            ctx.didFail = true;
            ctx.info    = $"Failed to parse Global ID {item.descriptor.info}...";

            return(null);
        }
示例#11
0
        internal static object DrawObjectReference(Rect rect, object value)
        {
            if (value is UnityEngine.Object obj)
            {
                DrawObjectField(rect, obj);
            }
            else if (value is string s && GlobalObjectId.TryParse(s, out var gid))
            {
                obj = GlobalObjectId.GlobalObjectIdentifierToObjectSlow(gid);
                if (obj)
                {
                    DrawObjectField(rect, obj);
                }
            }

            return(value?.ToString());
        }
示例#12
0
    /// <summary>
    /// Do not call this in OnBeforeSerialize/OnAfterDeserialize or you will crash!
    /// </summary>
    /// <returns></returns>
    public T Load <T>() where T : Object
    {
        if (string.IsNullOrEmpty(_guid))
        {
            return(null);
        }

#if UNITY_EDITOR
        var str = $"GlobalObjectId_V1-{_typeId}-{_guid}-{(ulong)_localId}-{(ulong)_prefabId}";

        if (GlobalObjectId.TryParse(str, out var id))
        {
            var obj = GlobalObjectId.GlobalObjectIdentifierToObjectSlow(id);
            Debug.Assert(Type == obj.GetType());
            return(obj as T);
        }

        return(default);
示例#13
0
        private static string FetchDescription(SearchItem item)
        {
            if (!GlobalObjectId.TryParse(item.id, out var gid))
            {
                return(null);
            }

            var go = GlobalObjectId.GlobalObjectIdentifierToObjectSlow(gid) as GameObject;

            if (go)
            {
                return(item.description = $"Source: {SearchUtils.GetHierarchyPath(go)}");
            }

            var sourceAssetPath = AssetDatabase.GUIDToAssetPath(gid.assetGUID.ToString());

            return(item.description = $"Source: {GetAssetDescription(sourceAssetPath)}");
        }
示例#14
0
        private static Texture2D FetchPreview(SearchItem item, FetchPreviewOptions options)
        {
            if (!GlobalObjectId.TryParse(item.id, out var gid))
            {
                return(null);
            }

            if (options.HasFlag(FetchPreviewOptions.Large))
            {
                var go = GlobalObjectId.GlobalObjectIdentifierToObjectSlow(gid) as GameObject;
                if (go)
                {
                    return(AssetPreview.GetAssetPreview(go));
                }
            }

            var sourceAssetPath = AssetDatabase.GUIDToAssetPath(gid.assetGUID.ToString());

            return(AssetDatabase.GetCachedIcon(sourceAssetPath) as Texture2D);
        }
示例#15
0
        private static Texture2D FetchPreview(SearchItem item, Vector2 size, FetchPreviewOptions options)
        {
            var info = GetInfo(item);

            if (info.gid.assetGUID == default)
            {
                return(null);
            }

            if (item.preview && item.preview.width >= size.x && item.preview.height >= size.y)
            {
                return(item.preview);
            }

            if (info.gid.identifierType == (int)IdentifierType.kSceneObject)
            {
                return(AssetDatabase.GetCachedIcon(info.source) as Texture2D);
            }

            if (info.gid.identifierType == (int)IdentifierType.kBuiltInAsset)
            {
                return(AssetPreview.GetAssetPreview(info.obj) ?? AssetPreview.GetMiniThumbnail(info.obj));
            }

            var obj = GlobalObjectId.GlobalObjectIdentifierToObjectSlow(info.gid);

            if (obj is GameObject go)
            {
                return(item.preview = Utils.GetSceneObjectPreview(go, size, options, item.thumbnail));
            }
            else if (obj)
            {
                var p = AssetPreview.GetAssetPreview(obj);
                if (p)
                {
                    return(p);
                }
            }

            return(item.preview = Utils.GetAssetPreviewFromPath(info.source, size, options));
        }
示例#16
0
        public void JsonSerialization_Serialize_UnityEngineObject_FromGlobalObjectId()
        {
            var src = new UnityEditorGlobalObjectIdContainer {
                Value = GlobalObjectId.GetGlobalObjectIdSlow(AssetDatabase.LoadAssetAtPath <Texture2D>(k_AssetPath))
            };
            var json = JsonSerialization.Serialize(src);

            Debug.Log(json);
            Assert.That(json, Does.Match(@".*""GlobalObjectId_V\d-\d-[\da-f]{32}-\d{7}-\d"".*"));

            var dst = new UnityEditorGlobalObjectIdContainer();

            using (JsonSerialization.DeserializeFromString(json, ref dst))
            {
                Assert.That(dst.Value, Is.Not.EqualTo(new GlobalObjectId()));

                var obj = GlobalObjectId.GlobalObjectIdentifierToObjectSlow(dst.Value);
                Assert.That(obj, Is.Not.Null);
                Assert.That(obj, Is.Not.False);
                Assert.That(AssetDatabase.GetAssetPath(obj), Is.EqualTo(k_AssetPath));
            }
        }
示例#17
0
        internal static IBuildStep Deserialize(string json)
        {
            if (string.IsNullOrEmpty(json))
            {
                return(null);
            }

            if (GlobalObjectId.TryParse(json, out var id))
            {
                if (GlobalObjectId.GlobalObjectIdentifierToObjectSlow(id) is BuildPipeline pipeline)
                {
                    return(pipeline);
                }
            }
            else
            {
                if (TypeConstruction.TryConstructFromAssemblyQualifiedTypeName <IBuildStep>(json, out var step))
                {
                    return(step);
                }
            }

            return(null);
        }
示例#18
0
        private bool Validate()
        {
            // Validate should return whether the popup is alive.
            // If not, it should try reinitializing it from serialized data, if possible.
            // If it returns false, the popup may have been closed by Validate, and the caller should abort.

            // If we're already alive, no need to do anything, just return true.
            var isAlive = editor != null &&
                          editor.targets.All(t => t != null) &&
                          editor.serializedObject != null &&
                          editor.serializedObject.targetObject != null &&
                          editor.serializedObject.targetObjects.All(t => t != null);

            if (isAlive)
            {
                return(true);
            }

            // If we don't have initialization data and we're not alive, we close and abort.
            // Normally though, this shouldn't happen, since this gets set in Initialize() then serialized.

            var isInitializable = initializationTargets != null
#if UNITY_2019_2_OR_NEWER
                                  && initializationTargetIdData != null &&
                                  initializationTargets.Length == initializationTargetIdData.Length
#endif
            ;

            if (!isInitializable)
            {
                Close();
                return(false);
            }

            // We wait for the plugin container to be initialized, but don't close yet if it isn't.
            if (!PluginContainer.initialized)
            {
                return(false);
            }

            // Check that we do want persistent editors. Currently an experimental feature.
            if (!PeekPlugin.Configuration.persistentPinnedEditors)
            {
                Close();
                return(false);
            }

            // The strategy will be to try to use the serialized object reference if available,
            // then otherwise parse and return the GOID. Normally we should be able to rely on
            // the GOID alone, but there's a bug (TODO: REPORT) in the UnityEditor.GUID.TryParse
            // function which will return false for zero values, the case for objects within
            // unsaved scenes.

            var targets = new List <UnityObject>();

            for (var i = 0; i < initializationTargets.Length; i++)
            {
                var initializationTarget = initializationTargets[i];

                // We have a direct alive reference, keep that
                if (initializationTarget != null)
                {
                    targets.Add(initializationTarget);
                    continue;
                }

#if UNITY_2019_2_OR_NEWER
                // Fallback to parsing the GOID

                var initializationTargetIdDatum = initializationTargetIdData[i];

                if (!GlobalObjectId.TryParse(initializationTargetIdDatum, out var initializationTargetId))
                {
                    continue;
                }

                var target = GlobalObjectId.GlobalObjectIdentifierToObjectSlow(initializationTargetId);

                if (target == null)
                {
                    continue;
                }

                targets.Add(target);
#endif
            }

            // If we couldn't find any initialization target that's still alive, we have nothing to display,
            // so we close and abort.
            if (targets.Count == 0)
            {
                Close();
                return(false);
            }

            // If we did find alive targets to reinitialize, we use those to reinitialize the popup and return true.
            Initialize(targets.ToArray());
            return(true);
        }
 public Object LoadObject()
 {
     return(targetObject ?? GlobalObjectId.GlobalObjectIdentifierToObjectSlow(globalObjectId));
 }