Example #1
0
        public IEnumerator LoadRepresentation(EntityData entityInfo)
        {
            isBusy = true;
            loadingSingleRepresentation = true;
            Debug.Log("Loading representation...");
            OnStartLoadingRepresentation();

            yield return(new WaitForEndOfFrame());

            EntityRepresentation representation = entityInfo.activeRepresentation;

            if (representation != null)
            {
                switch (entityInfo.activeRepresentation.assetType)
                {
                case EntityRepresentation.AssetType.Model:
                    ModelLoader importer = GetComponent <ModelLoader>();
                    if (importer == null)
                    {
                        importer = gameObject.AddComponent <ModelLoader>();
                        //importer.CreatedModel += OnModelCreated;
                        //importer.ImportedModel += OnModelImported;
                        //importer.ImportError += OnModelError;
                    }
                    importer.ImportModelAsync(representation.name, representation.assetName, representation.transform, representation.importOptions);
                    while (importer.Running)
                    {
                        yield return(null);
                    }
                    break;

                case EntityRepresentation.AssetType.AssetBundle:
                case EntityRepresentation.AssetType.Prefab:
                case EntityRepresentation.AssetType.Primitive:
                case EntityRepresentation.AssetType.None:
                    //throw new NotImplementedException();
                    break;
                }

                yield return(new WaitForEndOfFrame());

                Debug.Log("Representation loaded.");
                loadingSingleRepresentation = false;
                isBusy = false;

                OnRepresentationLoaded();
            }
        }
Example #2
0
        protected void OnModelCreated(GameObject obj, string absolutePath)
        {
            if (obj.transform.parent == null)
            {
                throw new System.NullReferenceException();
            }
            GameObject parentObj = obj.transform.parent.gameObject;
            EntityData entity    = parentObj.GetComponent <EntityData>();

            if (entity == null)
            {
                throw new System.NullReferenceException();
            }
            // obj.name == representation.id
            //createdRepresentations++;
            //throw new System.NotImplementedException();
            EntityRepresentation entityRepr = obj.AddComponent <EntityRepresentation>();

            entityRepr.assetName = absolutePath;
        }
        public static EntityRepresentation MakeGameObjectRepresentation(GameObject go)
        {
            EntityRepresentation entityRepr = null;

            if (go)
            {
                entityRepr = go.GetComponent <EntityRepresentation>();
                if (entityRepr == null)
                {
                    // Register the creation in the undo system
                    entityRepr           = Undo.AddComponent <EntityRepresentation>(go);
                    entityRepr.assetType = EntityRepresentation.AssetType.None;
                    var prefab = PrefabUtility.GetPrefabInstanceHandle(go);
                    if (prefab)
                    {
                        Debug.LogFormat("Prefab of {0} is a {1}: {2}", go.name, prefab.GetType().Name, prefab.name);
                    }
                    GameObject prefabParent = PrefabUtility.GetCorrespondingObjectFromSource(go) as GameObject;
                    if (prefabParent)
                    {
                        entityRepr.assetType = EntityRepresentation.AssetType.Prefab;
                        Debug.LogFormat("Prefab parent of {0} is a {1}: {2}", go.name, PrefabUtility.GetPrefabAssetType(prefabParent), prefabParent.name);
                        string prefabParentPath = AssetDatabase.GetAssetPath(prefabParent);

                        entityRepr.assetBundleName = null;

                        /*
                         * Object prefabAncestor = prefabParent;
                         * while (prefabAncestor)
                         * {
                         * Debug.LogFormat("Path: {0}", AssetDatabase.GetAssetPath(prefabAncestor));
                         *  string path = AssetDatabase.GetAssetPath(prefabAncestor);
                         *  AssetImporter assetImporter = AssetImporter.GetAtPath(path);
                         *  if (!string.IsNullOrEmpty(entityRepr.assetBundleName))
                         *  {
                         *      entityRepr.assetType = EntityRepresentation.AssetType.AssetBundle;
                         *      entityRepr.assetBundleName = assetImporter.assetBundleName;
                         *      break;
                         *  }
                         * Debug.LogFormat("Prefab parent of {0} is a {1}: {2}", go.name, PrefabUtility.GetPrefabType(prefabParent), prefabParent.name);
                         *  prefabAncestor = PrefabUtility.GetPrefabParent(prefabAncestor);
                         * }*/
                        AssetDatabase.GetMainAssetTypeAtPath(prefabParentPath);
                        string[] assetBundleNames = AssetDatabase.GetAllAssetBundleNames();
                        foreach (string abName in assetBundleNames)
                        {
                            string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundle(abName);
                            if (assetPaths.Contains(prefabParentPath))
                            {
                                entityRepr.assetType       = EntityRepresentation.AssetType.AssetBundle;
                                entityRepr.assetBundleName = abName;
                                int dotPos = entityRepr.assetBundleName.IndexOf('.');
                                if (dotPos > 0)
                                {
                                    entityRepr.assetBundleName = entityRepr.assetBundleName.Substring(0, dotPos);
                                }
                                break;
                            }
                        }
                        entityRepr.assetName = prefabParent.name;
                    }
                    else
                    {
                        if (entityRepr.transform.childCount == 0)
                        {
                            MeshFilter meshFilter = go.GetComponent <MeshFilter>();
                            if (meshFilter)
                            {
                                entityRepr.assetType = EntityRepresentation.AssetType.Primitive;
                                Mesh mesh = meshFilter.sharedMesh;
                                if (mesh)
                                {
                                    switch (mesh.name)
                                    {
                                    case "Cube":
                                        entityRepr.assetPrimType = EntityRepresentation.AssetPrimType.Cube;
                                        break;

                                    case "Sphere":
                                        entityRepr.assetPrimType = EntityRepresentation.AssetPrimType.Sphere;
                                        break;

                                    case "Cylinder":
                                        entityRepr.assetPrimType = EntityRepresentation.AssetPrimType.Cylinder;
                                        break;

                                    case "Capsule":
                                        entityRepr.assetPrimType = EntityRepresentation.AssetPrimType.Capsule;
                                        break;

                                    case "Quad":
                                        entityRepr.assetPrimType = EntityRepresentation.AssetPrimType.Quad;
                                        break;

                                    case "Plane":
                                        entityRepr.assetPrimType = EntityRepresentation.AssetPrimType.Plane;
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            Debug.LogError("Cannot create a representation from a hierarchy of primitives, create a prefab or an asset bundle and create the representation from it.");
                        }
                    }
                }
            }
            Transform parent = entityRepr.gameObject.transform.parent;

            if (parent)
            {
                var entity = parent.gameObject.GetComponent <EntityData>();
                if (entity)
                {
                    if (entity.activeRepresentation == null)
                    {
                        entity.activeRepresentation = entityRepr;
                    }
                }
                else
                {
                    Debug.LogWarning("The parent of this representation is not an entity.");
                }
            }
            else
            {
                Debug.LogWarning("This representation must be attached to an entity object.");
            }
            return(entityRepr);
        }
Example #4
0
        protected IEnumerator CreateRepresentationObject(
            EntityData entityData,
            string representationId,
            string representationAssetName,
            string representationAssetBundleName,
            EntityRepresentation.AssetType representationAssetType,
            EntityRepresentation.AssetPrimType representationAssetPrimType,
            ModelImportOptions importOptions
            )
        {
            bool created = false;
            EntityRepresentation entityRepresentation = null;

            //if (!string.IsNullOrEmpty(representationId))
            {
                PrimitiveType?primitiveCreated = null;
                EntityRepresentation.AssetPrimType primType = EntityRepresentation.AssetPrimType.None;
                switch (representationAssetType)
                {
                //case EntityRepresentation.AssetType.AssetBundle:
                //    if (!string.IsNullOrEmpty(representationAssetBundleName)
                //        && !string.IsNullOrEmpty(representationAssetName))
                //    {
                //        yield return InstantiateGameObjectAsync(representationAssetBundleName, representationAssetName, representationId, entityData.id);
                //        created = true;
                //    }
                //    break;
                case EntityRepresentation.AssetType.Model:
                    if (!string.IsNullOrEmpty(representationAssetName))
                    {
                        ModelLoader importer = GetComponent <ModelLoader>();
                        if (importer == null)
                        {
                            importer = gameObject.AddComponent <ModelLoader>();
                            importer.CreatedModel  += OnModelCreated;
                            importer.ImportedModel += OnModelImported;
                            importer.ImportError   += OnModelError;
                        }
                        importer.ImportModelAsync(representationId, representationAssetName, entityObjects[entityData.id].transform, importOptions);
                        while (importer.Running)
                        {
                            yield return(null);
                        }
                        EntityRepresentation entityRepr = null;
                        while (entityRepr == null && !importer.ErrorOccurred)
                        {
                            yield return(null);

                            entityRepr = entityObjects[entityData.id].GetComponentInChildren <EntityRepresentation>();
                        }
                        if (entityRepr != null)
                        {
                            entityRepresentation = entityRepr;
                            while (entityRepr.assetType != EntityRepresentation.AssetType.Model)
                            {
                                yield return(null);
                            }
                            entityRepr.assetName     = representationAssetName;
                            entityRepr.importOptions = importOptions;
                            entityObjects[entityData.id].GetComponent <EntityData>().activeRepresentation = entityRepr;

                            created = true;
                        }
                    }
                    break;

                case EntityRepresentation.AssetType.AssetBundle:
                case EntityRepresentation.AssetType.Prefab:
                    if (!string.IsNullOrEmpty(representationAssetName))
                    {
                        // TODO: this must be documented
                        string folder = "Representations/";
                        if (!string.IsNullOrEmpty(representationAssetBundleName))
                        {
                            folder += representationAssetBundleName + "/";
                        }
                        string     resourceName = folder + representationAssetName;
                        GameObject go           = null;
                        try
                        {
                            go = Instantiate(Resources.Load(resourceName), Vector3.zero, Quaternion.identity) as GameObject;
                        }
                        catch (Exception e)
                        {
                            Debug.LogError($"Failed to load resource {resourceName}: {e}");
                        }
                        if (go)
                        {
                            go.transform.SetParent(entityData.transform, false);
                            EntityRepresentation entityRepr = go.GetComponent <EntityRepresentation>();
                            if (entityRepr == null)
                            {
                                entityRepr = go.AddComponent <EntityRepresentation>();
                            }
                            go.name              = representationId;
                            entityRepr.name      = representationAssetName;
                            entityRepr.assetName = representationAssetName;
                            created              = true;
                            entityRepresentation = entityRepr;
                        }
                    }
                    break;

                case EntityRepresentation.AssetType.Primitive:
                    primitiveCreated = ConvertEntityToUnityPrimType(representationAssetPrimType);
                    primType         = representationAssetPrimType;
                    break;
                }
                if (primitiveCreated.HasValue)
                {
                    GameObject go = GameObject.CreatePrimitive(primitiveCreated.Value);
                    if (go)
                    {
                        go.transform.SetParent(entityData.transform, false);
                        EntityRepresentation entityRepr = go.GetComponent <EntityRepresentation>();
                        if (entityRepr == null)
                        {
                            entityRepr = go.AddComponent <EntityRepresentation>();
                        }
                        entityRepr.assetType     = EntityRepresentation.AssetType.Primitive;
                        entityRepr.assetPrimType = primType;
                        entityRepr.name          = representationId;
                        if (string.IsNullOrEmpty(entityRepr.name))
                        {
                            entityRepr.name = primitiveCreated.Value.ToString();
                        }
                        entityRepr.assetBundleName = null;
                        entityRepr.assetName       = null;
                        created = true;
                        entityRepresentation = entityRepr;
                    }
                }
            }

            if (!created)
            {
                if (representationAssetType != EntityRepresentation.AssetType.None)
                {
                    if (string.IsNullOrEmpty(representationAssetName))
                    {
                        Debug.LogWarning($"Asset information missing for the representation {representationId}");
                    }
                    else
                    {
                        Debug.LogWarning($"Failed to load representation from asset {representationAssetName}");
                        // TODO: Add a symbolic representation (cube/text)?
                    }
                }
            }
            if (entityRepresentation)
            {
                entityData.activeRepresentation = entityRepresentation;
                createdRepresentations++;
            }
        }