public SoundSystem.SoundHandle Play(WeakAssetReference weakSoundDef, Vector3 position)
    {
        var soundDef = m_SoundRegistry.GetSoundDef(weakSoundDef);

        if (soundDef == null)
        {
            GameDebug.LogWarning("Trying to play sound with asset ref " + weakSoundDef.ToGuidStr() + " but it is not in the registry");
            return(new SoundSystem.SoundHandle());
        }
        return(Play(soundDef, position));
    }
Exemple #2
0
    public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
    {
        // Figure out what asset types we allow. Default to all.
        var assetType = typeof(Object);

        if (fieldInfo != null)
        {
            AssetTypeAttribute assetTypeProperty = System.Attribute.GetCustomAttribute(fieldInfo, typeof(AssetTypeAttribute)) as AssetTypeAttribute;
            assetType = assetTypeProperty != null ? assetTypeProperty.assetType : typeof(Object);
        }

        var val0 = prop.FindPropertyRelative("val0");
        var val1 = prop.FindPropertyRelative("val1");
        var val2 = prop.FindPropertyRelative("val2");
        var val3 = prop.FindPropertyRelative("val3");

        var reference = new WeakAssetReference(val0.intValue, val1.intValue, val2.intValue, val3.intValue);

        var    guidStr = "";
        Object obj     = null;

        if (reference.IsSet())
        {
            guidStr = reference.ToGuidStr();
            var path = AssetDatabase.GUIDToAssetPath(guidStr);

            if (assetType == null)
            {
                assetType = AssetDatabase.GetMainAssetTypeAtPath(path);
            }

            obj = AssetDatabase.LoadAssetAtPath(path, assetType);
        }

        pos = EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), new GUIContent(label.text + "(" + guidStr + ")"));
        Object newObj = EditorGUI.ObjectField(pos, obj, assetType, false);

        if (newObj != obj)
        {
            var newRef = new WeakAssetReference();
            if (newObj != null)
            {
                var path = AssetDatabase.GetAssetPath(newObj);
                newRef = new WeakAssetReference(AssetDatabase.AssetPathToGUID(path));
            }

            val0.intValue = newRef.val0;
            val1.intValue = newRef.val1;
            val2.intValue = newRef.val2;
            val3.intValue = newRef.val3;
        }
    }
    public static Entity CreateEntity(EntityManager entityManager, EntityCommandBuffer cmdBuffer, WeakAssetReference assetGuid)
    {
        var entityPrefab = PrefabAssetRegistry.FindEntityPrefab(entityManager, assetGuid);

        if (entityPrefab == Entity.Null)
        {
            GameDebug.LogError("Failed to create prefab for asset:" + assetGuid.ToGuidStr());
            return(Entity.Null);
        }
        var instance = cmdBuffer.Instantiate(entityPrefab);

        return(instance);
    }
Exemple #4
0
    public void AddReference(WeakAssetReference reference)
    {
        if (!reference.IsSet())
        {
            return;
        }

        if (references.Contains(reference))
        {
            return;
        }

        var path = AssetDatabase.GUIDToAssetPath(reference.ToGuidStr());

        if (String.IsNullOrEmpty(path))
        {
            GameDebug.LogWarning("Asset does not exist:" + reference.ToGuidStr());
            return;
        }

        GameDebug.Log("Adding asset:" + reference.ToGuidStr() + " " + path);

        references.Add(reference);
    }
Exemple #5
0
    public static Entity CreateEntity(EntityManager entityManager, WeakAssetReference assetGuid)
    {
        var entityPrefab = PrefabAssetRegistry.FindEntityPrefab(entityManager, assetGuid);

        if (entityPrefab == Entity.Null)
        {
            GameDebug.LogError("Failed to create prefab for asset:" + assetGuid.ToGuidStr());
            return(Entity.Null);
        }
        var e = entityManager.Instantiate(entityPrefab);

        GameDebug.Log(entityManager.World, ShowLifetime, "Created entity:{0} from asset:{1}", e, assetGuid.ToGuidStr());

        return(e);
    }
Exemple #6
0
        protected override void OnUpdate()
        {
            // Camera.main may not be available when the system is created, so need this to set it for the first time
            if (MainCamera == null)
            {
                if (Camera.main != null)
                {
                    MainCamera = Camera.main;
                }
                else
                {
                    GameDebug.LogWarning("PartOwner update: No camera.main");
                    return;
                }
            }

            var camPos = (float3)MainCamera.transform.position;

            // TODO: Jobified ForEach blocked by PrefabAssetRegistry.CreateEntity
            Entities.ForEach((Entity partOwnerEntity, ref Translation translation, ref RegistryAsset registryAsset, ref InputState inputState,
                              ref State state) =>
            {
                var registry = PartRegistry.GetPartRegistry(World, registryAsset.Value);

                // Calc lod
                var charPos = translation.Value;
                var dist    = math.distance(camPos, charPos);
                var newLod  = -1;
                // TODO (mogensh) add threshold that needs to be passed before change (so it does not flicker)
                for (int lod = 0; lod < registry.Value.LODLevels.Length; lod++)
                {
                    if (dist <= registry.Value.LODLevels[lod].EndDist)
                    {
                        newLod = lod;
                        break;
                    }
                }


                // TODO (mogensh) hack: force LOD 0
                newLod = 0;


                // Handle out of lod distance specifically
                if (newLod == -1)
                {
                    if (state.currentLOD != newLod)
                    {
                        state.currentLOD = newLod;

                        GameDebug.Log(Part.ShowLifetime, "Out of LOD distance");

                        var partBuf = EntityManager.GetBuffer <PartElement>(partOwnerEntity)
                                      .ToNativeArray(Allocator.Temp);
                        var partOutBuf = PostUpdateCommands.SetBuffer <PartElement>(partOwnerEntity);
                        partOutBuf.ResizeUninitialized(partBuf.Length);
                        for (int j = 0; j < partBuf.Length; j++)
                        {
                            var partElement = partBuf[j];

                            // Destroy old part
                            if (partElement.PartId != 0)
                            {
                                if (partElement.PartEntity != Entity.Null)
                                {
                                    GameDebug.Log(Part.ShowLifetime, "Destroying part. Category:{0} partId:{1}", j,
                                                  partElement.PartId);
                                    PostUpdateCommands.DestroyEntity(partElement.PartEntity);
                                }

                                partElement.PartEntity = Entity.Null;
                                partElement.PartId     = 0;
                                partElement.Asset      = WeakAssetReference.Default;
                            }

                            partOutBuf[j] = partElement;
                        }

                        PostUpdateCommands.SetComponent(partOwnerEntity, state);
                    }


                    return;
                }



                var newRig = BlobAssetReference <RigDefinition> .Null;
                if (EntityManager.HasComponent <SharedRigDefinition>(partOwnerEntity))
                {
                    newRig = EntityManager.GetSharedComponentData <SharedRigDefinition>(partOwnerEntity).Value;
                }


                // Change bodypart if LOD or rig changed
                var packedPartIds = inputState.PackedPartIds;
                if (packedPartIds != state.CurrentPackedPartIds ||
                    newLod != state.currentLOD ||
                    (newRig != BlobAssetReference <RigDefinition> .Null && newRig != state.currentRig))
                {
                    var partBuf = EntityManager.GetBuffer <PartElement>(partOwnerEntity).ToNativeArray(Allocator.Temp);


                    var partIds = new NativeArray <int>(partBuf.Length, Allocator.Temp);
                    registry.Value.UnpackPartsList(inputState.PackedPartIds, partIds);

                    GameDebug.Log(World, Part.ShowLifetime, "Property changed. Lod:{0}", newLod);

                    var partOutBuf = PostUpdateCommands.SetBuffer <PartElement>(partOwnerEntity);
                    partOutBuf.ResizeUninitialized(partBuf.Length);
                    for (int j = 0; j < partBuf.Length; j++)
                    {
                        var partId      = partIds[j];
                        var partElement = partBuf[j];

                        // Find new asset given the new properties
                        var asset = new WeakAssetReference();
                        if (partId > 0)
                        {
                            var skeletonHash = newRig.IsCreated ? newRig.Value.GetHashCode() : 0;
                            var found        = registry.Value.FindAsset(j, partId, skeletonHash, newLod, ref asset);
                            if (!found)
                            {
                                GameDebug.Log(World, Part.ShowLifetime,
                                              "Failed to find valid part. Category:{0} PartId:{1}", j, partId);
                            }
                        }


                        // No change if asset has not changed
                        if (partElement.Asset == asset)
                        {
                            partOutBuf[j] = partElement;
                            continue;
                        }


                        // Destroy old part
                        if (partElement.PartId != 0)
                        {
                            if (partElement.PartEntity != Entity.Null)
                            {
                                GameDebug.Log(World, Part.ShowLifetime, "Destroying part. Category:{0} partId:", j,
                                              partElement.PartId);
                                PostUpdateCommands.DestroyEntity(partElement.PartEntity);
                            }

                            partElement.PartEntity = Entity.Null;
                            partElement.PartId     = 0;
                            partElement.Asset      = WeakAssetReference.Default;
                        }

                        // Create new part
                        if (partId != 0 && asset.IsSet())
                        {
                            partElement.PartEntity = PrefabAssetManager.CreateEntity(EntityManager, asset);
                            partElement.PartId     = partId;
                            partElement.Asset      = asset;

                            if (partElement.PartEntity != Entity.Null)
                            {
                                GameDebug.Log(World, Part.ShowLifetime,
                                              "Creating part. Owner:{0} Cat:{1} PartId:{2} Asset:{3} part:{4}", partOwnerEntity,
                                              j, partId, asset.ToGuidStr(), partElement.PartEntity);
                                var part   = Part.Owner.Default;
                                part.Value = partOwnerEntity;
                                PostUpdateCommands.SetComponent(partElement.PartEntity, part);

                                // TODO (mogensh) add "static" property on owner (or get somehow). If static just set world transform
                                PostUpdateCommands.AddComponent(partElement.PartEntity,
                                                                new Parent {
                                    Value = partOwnerEntity
                                });
                                PostUpdateCommands.AddComponent(partElement.PartEntity, new LocalToParent());
                            }
                            else
                            {
                                GameDebug.LogError("Failed to create part. Asset:" + asset.ToGuidStr());
                            }
                        }

                        partOutBuf[j] = partElement;
                    }

                    state.CurrentPackedPartIds = packedPartIds;
                    state.currentRig           = newRig;
                    state.currentLOD           = newLod;
                    PostUpdateCommands.SetComponent(partOwnerEntity, state);
                }
            });
        }