Beispiel #1
0
        private IEnumerator LoadInstance(FamilyInstance f)
        {
            this.instanceData = f;
            this.InstanceData = Newtonsoft.Json.JsonConvert.SerializeObject(f);
            this.fam          = FamilyLibrary.GetFamily(f.FamilyId);
            if (this.fam == null)
            {
                throw new System.Exception("Family " + f.FamilyId + " is not loaded");
            }
            if (f.HostId != null)
            {
                this.host = GeometryLibrary.GetObject(f.HostId);
            }

#if UNITY_EDITOR
            this.name = $"Family ({f.Id} - {this.fam.Tag} - {this.fam.ModelName ?? this.fam.FamilyName})";
#else
            this.name = f.Id;
#endif

            // GameObject model = (GameObject)Resources.Load($"Families/{this.fam.Name}/model");
            CoroutineWithData <object> cd = new CoroutineWithData <object>(this, FamilyLibrary.ResolveFamilyOBJ(f.FamilyId, f.VariantId));
            yield return(cd.coroutine);

            object result = cd.result;

            if (result != null)
            {
                byte[] objData = cd.result as byte[];

                // Debug.Log("PLACING FAMILY");

                GameObject modelInstance;
                using (var textStream = new MemoryStream(objData))
                {
                    modelInstance = new OBJLoader().Load(textStream);
                }

                var initialRotation = modelInstance.transform.localRotation;

                modelInstance.transform.parent        = this.transform;
                modelInstance.transform.localPosition = Vector3.zero;
                modelInstance.transform.localRotation = initialRotation;
                modelInstance.transform.localScale    = new Vector3(
                    Helpers.Constants.M_PER_FT,
                    Helpers.Constants.M_PER_FT,
                    Helpers.Constants.M_PER_FT
                    );

                foreach (var l in modelInstance.GetComponentsInChildren <Light>())
                {
                    l.gameObject.transform.rotation = Quaternion.LookRotation(Vector3.down);
                }

                foreach (UnityEngine.Transform child in transform)
                {
                    foreach (UnityEngine.Transform geo in child)
                    {
                        var c = geo.GetComponent <FamilyGeometryController>();
                        if (c != null)
                        {
                            c.CollisionEnter += this.OnCollsionEnterEvent;
                            c.CollisionExit  += this.OnCollisionExitEvent;
                        }
                    }
                }
                // Debug.Log("Children " + count);
            }

#if UNITY_EDITOR
            StartCoroutine(CheckForUpdate());
#endif
        }