private void UpdateDependantAssetPreviews(AssetItem[] assetItems, Action callback)
        {
            IResourcePreviewUtility previewUtil = IOC.Resolve <IResourcePreviewUtility>();

            AssetItem[] dependantItems = m_project.GetDependantAssetItems(assetItems).Where(item => !m_project.IsScene(item)).ToArray();
            if (dependantItems.Length > 0)
            {
                m_project.Load(dependantItems, (loadError, loadedObjects) =>
                {
                    if (loadError.HasError)
                    {
                        IsBusy = false;
                        m_wm.MessageBox("Unable to load assets", loadError.ErrorText);
                        return;
                    }

                    for (int i = 0; i < loadedObjects.Length; ++i)
                    {
                        UnityObject loadedObject = loadedObjects[i];
                        AssetItem dependantItem  = dependantItems[i];
                        if (loadedObject != null)
                        {
                            byte[] previewData    = previewUtil.CreatePreviewData(loadedObject);
                            dependantItem.Preview = new Preview {
                                ItemID = dependantItem.ItemID, PreviewData = previewData
                            };
                        }
                        else
                        {
                            dependantItem.Preview = new Preview {
                                ItemID = dependantItem.ItemID
                            };
                        }
                    }

                    m_project.SavePreview(dependantItems, (savePreviewError, savedAssetItems) =>
                    {
                        if (savePreviewError.HasError)
                        {
                            IsBusy = false;
                            m_wm.MessageBox("Unable to load assets", savePreviewError.ErrorText);
                            return;
                        }

                        callback();
                    });
                });
            }
            else
            {
                callback();
            }
        }
Beispiel #2
0
        private IEnumerator CreatePreviewForLoadedResources(ProjectItem[] items)
        {
            if (m_resourcePreview == null)
            {
                yield break;
            }

            for (int i = 0; i < items.Length; ++i)
            {
                ProjectItem projectItem = items[i];
                if (projectItem is AssetItem)
                {
                    AssetItem   assetItem = (AssetItem)projectItem;
                    UnityObject obj;
                    if (m_project.IsStatic(projectItem))
                    {
                        if (!m_staticReferencesMapping.PersistentIDtoObj.TryGetValue(unchecked ((int)assetItem.ItemID), out obj))
                        {
                            obj = null;
                        }
                    }
                    else
                    {
                        if (assetItem.Preview == null && m_assetDB != null)
                        {
                            obj = m_assetDB.FromID <UnityObject>(assetItem.ItemID);
                        }
                        else
                        {
                            obj = null;
                        }
                    }


                    if (obj != null)
                    {
                        assetItem.Preview = new Preview
                        {
                            ItemID      = assetItem.ItemID,
                            PreviewData = m_resourcePreview.CreatePreviewData(obj)
                        };
                    }

                    yield return(new WaitForSeconds(0.01f));
                }
            }
        }
Beispiel #3
0
        protected override void OnEditorExist()
        {
            base.OnEditorExist();

            if (!FirstRun)
            {
                return;
            }
            FirstRun = false;

            IProject project = IOC.Resolve <IProject>();
            IRTE     editor  = IOC.Resolve <IRTE>();

            project.LoadImportItems("DemoAssetLibrary", true, (error, root) =>
            {
                if (error.HasError)
                {
                    editor.IsBusy = false;
                    Debug.LogError(error.ToString());
                }
                else
                {
                    IResourcePreviewUtility resourcePreview = IOC.Resolve <IResourcePreviewUtility>();
                    ImportItem[] importItems = root.Flatten(true).OfType <ImportItem>().ToArray();
                    for (int i = 0; i < importItems.Length; ++i)
                    {
                        ImportItem importItem = importItems[i];
                        importItem.Preview    = new Preview
                        {
                            ItemID      = importItem.ItemID,
                            PreviewData = resourcePreview.CreatePreviewData(importItems[i].Object)
                        };
                    }
                    project.UnloadImportItems(root);

                    project.Import(importItems, (importError, assetItems) =>
                    {
                        if (importError.HasError)
                        {
                            Debug.LogError(importError.HasError);
                        }

                        editor.IsBusy = false;
                    });
                }
            });
        }
Beispiel #4
0
        public override IEnumerator Import(string filePath, string targetPath)
        {
            byte[] bytes = File.ReadAllBytes(filePath);

            Texture2D texture = new Texture2D(4, 4);

            if (texture.LoadImage(bytes, false))
            {
                IProject project = IOC.Resolve <IProject>();
                IResourcePreviewUtility previewUtility = IOC.Resolve <IResourcePreviewUtility>();
                byte[] preview = previewUtility.CreatePreviewData(texture);
                yield return(project.Save(targetPath, texture, preview));
            }
            else
            {
                Debug.LogError("Unable to load image " + filePath);
            }

            Object.Destroy(texture);
        }
        public void CreateAsset(UnityObject asset, ProjectItem parentFolder)
        {
            IResourcePreviewUtility resourcePreview = IOC.Resolve <IResourcePreviewUtility>();

            byte[] preview = resourcePreview.CreatePreviewData(asset);
            Editor.IsBusy = true;
            m_project.Save(new[] { parentFolder }, new[] { preview }, new[] { asset }, null, (error, assetItems) =>
            {
                if (parentFolder != m_folders.FirstOrDefault())
                {
                    if (ItemDoubleClick != null)
                    {
                        ItemDoubleClick(this, new ProjectTreeEventArgs(new[] { parentFolder }));
                    }
                }

                Editor.ActivateWindow(this);
                Destroy(asset);
            });
        }
        public override IEnumerator Import(string filePath, string targetPath)
        {
            byte[] bytes = File.ReadAllBytes(filePath);

            Texture2D texture = new Texture2D(4, 4);

            if (texture.LoadImage(bytes, false))
            {
                if (texture.format == TextureFormat.RGBA32 || texture.format == TextureFormat.ARGB32)
                {
                    bool      opaque = true;
                    Color32[] pixels = texture.GetPixels32();
                    for (int i = 0; i < pixels.Length; ++i)
                    {
                        if (pixels[i].a != 255)
                        {
                            opaque = false;
                            break;
                        }
                    }

                    if (opaque)
                    {
                        texture.LoadImage(texture.EncodeToJPG(), false);
                    }
                }

                IProject project = IOC.Resolve <IProject>();
                IResourcePreviewUtility previewUtility = IOC.Resolve <IResourcePreviewUtility>();
                byte[] preview = previewUtility.CreatePreviewData(texture);
                yield return(project.Save(targetPath, texture, preview));
            }
            else
            {
                Debug.LogError("Unable to load image " + filePath);
            }

            Object.Destroy(texture);
        }
        private void OnTreesDatabinding(object sender, VirtualizingTreeViewItemDataBindingArgs e)
        {
            TreePrototype tree  = (TreePrototype)e.Item;
            RawImage      image = e.ItemPresenter.GetComponentInChildren <RawImage>();

            if (image != null)
            {
                if (image.texture != null && image.texture.name == "TemporaryPreview")
                {
                    Destroy(image.texture);
                }

                if (tree.prefab != null)
                {
                    IResourcePreviewUtility previewUtil = IOC.Resolve <IResourcePreviewUtility>();
                    Texture2D texture = new Texture2D(1, 1, TextureFormat.ARGB32, true);
                    texture.name = "TemporaryPreview";
                    texture.LoadImage(previewUtil.CreatePreviewData(tree.prefab));
                    image.texture = texture;
                }
            }
        }
Beispiel #8
0
        private void CreatePrefabWithDependencies(ProjectItem dropTarget, ExposeToEditor dragObject, Action <AssetItem[]> done)
        {
            IResourcePreviewUtility previewUtility = IOC.Resolve <IResourcePreviewUtility>();

            m_project.GetDependencies(dragObject.gameObject, true, (error, deps) =>
            {
                object[] objects;
                if (!deps.Contains(dragObject.gameObject))
                {
                    Debug.Log(dragObject.gameObject);
                    objects = new object[deps.Length + 1];
                    objects[deps.Length] = dragObject.gameObject;
                    for (int i = 0; i < deps.Length; ++i)
                    {
                        objects[i] = deps[i];
                    }
                }
                else
                {
                    objects = deps;
                }

                IUnityObjectFactory uoFactory = IOC.Resolve <IUnityObjectFactory>();
                objects = objects.Where(obj => uoFactory.CanCreateInstance(obj.GetType())).ToArray();

                byte[][] previewData = new byte[objects.Length][];
                for (int i = 0; i < objects.Length; ++i)
                {
                    if (objects[i] is UnityObject)
                    {
                        previewData[i] = previewUtility.CreatePreviewData((UnityObject)objects[i]);
                    }
                }
                CreatePrefab(dropTarget, previewData, objects, done);
            });
        }
        public static IEnumerator CoCreatePreviews(ProjectItem[] items, IProject project, IResourcePreviewUtility resourcePreview, Action done = null)
        {
            if (resourcePreview == null)
            {
                if (done != null)
                {
                    done();
                }
                yield break;
            }

            IRTE rte = IOC.Resolve <IRTE>();

            if (rte.Selection.activeObject != null)
            {
                long        id = project.ToID(rte.Selection.activeObject);
                ProjectItem selectedProjectItem = items.Where(item => item.ItemID == id).FirstOrDefault();
                if (selectedProjectItem != null && selectedProjectItem is AssetItem)
                {
                    AssetItem selectedAssetItem = (AssetItem)selectedProjectItem;
                    selectedAssetItem.Preview = null;
                }
            }

            for (int i = 0; i < items.Length; ++i)
            {
                ImportItem importItem = items[i] as ImportItem;
                if (importItem != null)
                {
                    if (/*importItem.Preview == null &&*/ importItem.Object != null)
                    {
                        importItem.Preview = new Preview
                        {
                            ItemID      = importItem.ItemID,
                            PreviewData = resourcePreview.CreatePreviewData(importItem.Object)
                        };
                    }
                }
                else
                {
                    AssetItem assetItem = items[i] as AssetItem;
                    if (assetItem != null)
                    {
                        UnityObject obj = null;
                        if (assetItem.Preview == null)
                        {
                            obj = project.FromID <UnityObject>(assetItem.ItemID);
                        }

                        if (obj != null)
                        {
                            assetItem.Preview = new Preview
                            {
                                ItemID      = assetItem.ItemID,
                                PreviewData = resourcePreview.CreatePreviewData(obj)
                            };
                        }
                    }
                }

                if (i % 10 == 0)
                {
                    yield return(new WaitForSeconds(0.005f));
                }
            }

            if (done != null)
            {
                done();
            }
        }
Beispiel #10
0
        public static IEnumerator CoCreatePreviews(ProjectItem[] items, IProject project, IResourcePreviewUtility resourcePreview)
        {
            if (resourcePreview == null)
            {
                yield break;
            }


            for (int i = 0; i < items.Length; ++i)
            {
                ImportItem importItem = items[i] as ImportItem;
                if (importItem != null)
                {
                    if (importItem.Preview == null && importItem.Object != null)
                    {
                        importItem.Preview = new Preview
                        {
                            ItemID      = importItem.ItemID,
                            PreviewData = resourcePreview.CreatePreviewData(importItem.Object)
                        };
                    }
                }
                else
                {
                    ImportItem assetItem = items[i] as ImportItem;
                    if (assetItem != null)
                    {
                        UnityObject obj;
                        if (project.IsStatic(assetItem))
                        {
                            #warning CommentedOut
                            //if (!project.TryGetFromStaticReferences(assetItem, out obj))
                            {
                                obj = null;
                            }
                        }
                        else
                        {
                            if (assetItem.Preview == null)
                            {
                                obj = project.FromID <UnityObject>(assetItem.ItemID);
                            }
                            else
                            {
                                obj = null;
                            }
                        }

                        if (obj != null)
                        {
                            assetItem.Preview = new Preview
                            {
                                ItemID      = assetItem.ItemID,
                                PreviewData = resourcePreview.CreatePreviewData(obj)
                            };
                        }
                    }
                }

                yield return(new WaitForSeconds(0.01f));
            }
        }