Beispiel #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();
            }
        }
Beispiel #2
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++;
            }
        }