Exemplo n.º 1
0
        /// <summary>
        /// Get a BlobAssetReference from its key
        /// </summary>
        /// <param name="key">The key associated with the BlobAssetReference</param>
        /// <param name="result">The BlobAssetReference if found or default</param>
        /// <typeparam name="T">The type of BlobAsset</typeparam>
        /// <returns>true if the BlobAssetReference was found, false if not found</returns>
        public bool TryAdd <T>(Hash128 key, BlobAssetReference <T> result) where T : struct
        {
            var typedHash = ComputeTypedHash(key, typeof(T));

            if (m_BlobAssets.ContainsKey(typedHash))
            {
                return(false);
            }

            m_BlobAssets.Add(typedHash, result.m_data);
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads bytes from a fileName, validates the expected serialized version, and deserializes them into a new blob asset.
        /// </summary>
        /// <param name="path">The path of the blob data to read.</param>
        /// <param name="version">Expected version number of the blob data.</param>
        /// <param name="result">The resulting BlobAssetReference if the data was read successful.</param>
        /// <returns>A bool if the read was successful or not.</returns>
        public static bool TryRead(string path, int version, out BlobAssetReference <T> result)
        {
            using (var binaryReader = new StreamBinaryReader(path))
            {
                var storedVersion = binaryReader.ReadInt();
                if (storedVersion != version)
                {
                    result = default;
                    return(false);
                }

                result = binaryReader.Read <T>();
                return(true);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Reads bytes from a buffer, validates the expected serialized version, and deserializes them into a new blob asset.
        /// </summary>
        /// <param name="buffer">Byte array of buffer</param>
        /// <param name="version">Expected version number of the blob data.</param>
        /// <param name="result">The resulting BlobAssetReference if the data was read successful.</param>
        /// <returns>A bool if the read was successful or not.</returns>
        public static bool TryRead(byte[] buffer, int version, out BlobAssetReference <T> result)
        {
            fixed(byte *fixedBuffer = buffer)
            {
                using (var binaryReader = new MemoryBinaryReader(fixedBuffer))
                {
                    var storedVersion = binaryReader.ReadInt();
                    if (storedVersion != version)
                    {
                        result = default;
                        return(false);
                    }

                    result = binaryReader.Read <T>();
                    return(true);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Try to access to a BlobAssetReference from its key
        /// </summary>
        /// <param name="key">The key associated with the BlobAssetReference when it was added to the cache</param>
        /// <param name="blobAssetReference">The corresponding BlobAssetReference or default if none was found</param>
        /// <typeparam name="T">The type of BlobAsset</typeparam>
        /// <returns></returns>
        public bool TryGet <T>(Hash128 key, out BlobAssetReference <T> blobAssetReference) where T : struct
        {
            var typedHash = ComputeTypedHash(key, typeof(T));

            var res = m_BlobAssets.TryGetValue(typedHash, out var blobData);

            if (res)
            {
                m_CacheHit++;

                blobAssetReference = BlobAssetReference <T> .Create(blobData);

                return(true);
            }

            m_CacheMiss++;
            blobAssetReference = default;
            return(false);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Adds a blob asset where the key that makes it unique is based on the BlobAsset contents itself. If the contents of the generated blob asset is the same as a previously inserted blob asset,
        /// then the passed blobAsset will be disposed and the reference to the blob asset will be replaced with the previously added blob asset
        /// </summary>
        /// <param name="blobAsset">The blob asset that will be inserted or replaced</param>
        /// <typeparam name="T"></typeparam>
        /// <returns>Returns true if the blob asset was added, returns false if the blob asset was disposed and replaced with the previous blob.</returns>
        unsafe public bool AddUniqueBlobAsset <T>(ref BlobAssetReference <T> blobAsset) where T : struct
        {
            Hash128 key  = default;
            ulong   hash = blobAsset.m_data.Header->Hash;

            UnsafeUtility.MemCpy(&key, &blobAsset.m_data.Header->Hash, sizeof(ulong));
            key.Value.w = ComputeTypeHash(typeof(T));

            if (m_BlobAssets.TryGetValue(key, out var previousBlob))
            {
                blobAsset.Dispose();
                blobAsset = BlobAssetReference <T> .Create(previousBlob);

                return(false);
            }

            m_BlobAssets.Add(key, blobAsset.m_data);
            return(true);
        }
Exemplo n.º 6
0
 public bool Equals(BlobAssetReference <T> other)
 {
     return(m_data.Equals(other.m_data));
 }
 /// <summary>
 /// Get the blob asset for the corresponding hash
 /// </summary>
 /// <param name="hash">The hash associated with the BlobAsset</param>
 /// <param name="blob">The BlobAsset corresponding to the given Hash</param>
 /// <returns>true if the blob asset was found, false otherwise</returns>
 public bool GetBlobAsset(Hash128 hash, out BlobAssetReference <TB> blob)
 {
     return(m_Computed.TryGetValue(hash, out blob) || m_BlobAssetStore.TryGet(hash, out blob));
 }
Exemplo n.º 8
0
    private UnityEngine.Material PlayerMaterial, EnemyMaterial; // = (Resources.Load("Prefabs/Player") as GameObject).GetComponent<MeshRenderer>().sharedMaterial;
    //[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
    void Start()
    {
        Debug.Log("GameManager Initilize();");
        PlayerMesh     = (Resources.Load("Prefabs/Player") as GameObject).GetComponent <MeshFilter>().sharedMesh;
        PlayerMaterial = (Resources.Load("Prefabs/Player") as GameObject).GetComponent <MeshRenderer>().sharedMaterial;

        EnemyMesh     = (Resources.Load("Prefabs/Enemy") as GameObject).GetComponent <MeshFilter>().sharedMesh;
        EnemyMaterial = (Resources.Load("Prefabs/Enemy") as GameObject).GetComponent <MeshRenderer>().sharedMaterial;

        var entityManager = World.Active.EntityManager;//GetOrCreateSystem<EntityManager>();

        //Player Spawn
        var playerArchetype = entityManager.CreateArchetype(
            typeof(LocalToWorld),
            typeof(Translation),
            typeof(Rotation),
            //typeof(Team),
            typeof(MovementSpeed),
            typeof(Health),
            typeof(MaxHealth),
            typeof(Power),
            typeof(DamageReduction),
            typeof(PlayerInput),
            typeof(Heading),
            //typeof(Target),
            typeof(RenderMesh),
            typeof(PhysicsCollider)
            );

        var player = entityManager.CreateEntity(playerArchetype);

        entityManager.SetSharedComponentData(player, new RenderMesh
        {
            mesh        = PlayerMesh,
            material    = PlayerMaterial,
            castShadows = ShadowCastingMode.On
        });

        Unity.Entities.BlobAssetReference <Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(float3.zero, quaternion.identity, new float3(1, 1, 1), 0);
        Unity.Physics.PhysicsCollider physicsCollider = new PhysicsCollider
        {
            Value = collider
        };
        entityManager.SetComponentData(player, physicsCollider);
        entityManager.SetComponentData(player, new Translation {
            Value = new float3(0, 0, 0)
        });
        //entityManager.SetComponentData(player, new UnitStats { team = 0, Health = 100, MaxHealth = new Stat(100), DamageReduction = new Stat(0), Power = new Stat(10), MovementSpeed = new Stat(1) });
        //entityManager.SetComponentData(player, new Team { Value = 0 });
        entityManager.SetComponentData(player, new MovementSpeed {
            Value = 1
        });
        //Bots Spawn
        var enemyArchetype = entityManager.CreateArchetype(
            typeof(LocalToWorld),
            typeof(Translation),
            typeof(Rotation),
            //typeof(Team),
            typeof(MovementSpeed),
            typeof(Health),
            typeof(MaxHealth),
            typeof(Power),
            typeof(DamageReduction),
            typeof(Heading),
            //typeof(Target),
            typeof(RenderMesh),
            typeof(PhysicsVelocity),
            typeof(PhysicsMass),
            typeof(PhysicsCollider)
            );

        NativeArray <Entity> enemyEntities = new NativeArray <Entity>(10000, Allocator.Temp);

        entityManager.CreateEntity(enemyArchetype, enemyEntities);

        RenderMesh enemyMeshRenderer = new RenderMesh
        {
            mesh        = EnemyMesh,
            material    = EnemyMaterial,
            castShadows = ShadowCastingMode.On
        };



        for (int i = 0; i < enemyEntities.Length; i++)
        {
            entityManager.SetComponentData(enemyEntities[i], physicsCollider);
            entityManager.SetComponentData(enemyEntities[i], PhysicsMass.CreateDynamic(MassProperties.UnitSphere, 1));
            entityManager.SetSharedComponentData(enemyEntities[i], enemyMeshRenderer);
            entityManager.SetComponentData(enemyEntities[i], new Translation {
                Value = new float3(UnityEngine.Random.Range(-100, 100), UnityEngine.Random.Range(-20, 20), UnityEngine.Random.Range(-100, 100))
            });
            entityManager.SetComponentData(enemyEntities[i], new Rotation {
                Value = quaternion.identity
            });
            entityManager.SetComponentData(enemyEntities[i], new MovementSpeed {
                Value = UnityEngine.Random.Range(0.1f, 5.0f)
            });
            entityManager.SetComponentData(enemyEntities[i], new Heading {
                Value = new float3(UnityEngine.Random.Range(-1.0f, 1.0f), 0, UnityEngine.Random.Range(-1.0f, 1.0f))
            });
        }

        enemyEntities.Dispose();

        Input.gyro.enabled        = true;
        Input.gyro.updateInterval = 0.02f;
    }