该项目的目的是同时测试和演示来自 Unity DOTS 技术堆栈的多个新包。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

77 行
2.2 KiB

using System;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
public class SoundRegistryAuthoring : MonoBehaviour, IConvertGameObjectToEntity
{
public List<SoundDef> soundDefs;
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
var sr = new SoundRegistry();
sr.soundDefs = soundDefs.ToArray();
sr.assetRefs = new WeakAssetReference[sr.soundDefs.Length];
#if UNITY_EDITOR
for(int i = 0; i < sr.soundDefs.Length; ++i)
{
sr.assetRefs[i] = new WeakAssetReference(AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(sr.soundDefs[i])));
}
#endif
dstManager.AddSharedComponentData(entity, sr);
}
}
// TODO: This could become an class IComponentData and we could get rid of IEquatable requirement
[Serializable]
public struct SoundRegistry : ISharedComponentData, IEquatable<SoundRegistry>
{
public SoundDef[] soundDefs;
public WeakAssetReference[] assetRefs;
public SoundDef GetSoundDef(WeakAssetReference assetRef)
{
if (assetRefs == null)
return null;
var i = assetRefs.IndexOf(assetRef);
return i > -1 ? soundDefs[i] : null;
}
public bool Equals(SoundRegistry other)
{
if ((soundDefs == null) && (other.soundDefs != null))
return false;
if ((assetRefs == null) && (other.assetRefs != null))
return false;
if (other.soundDefs.Length != soundDefs.Length)
return false;
if (other.assetRefs.Length != assetRefs.Length)
return false;
for(int i = 0, c = assetRefs.Length; i < c; ++i)
{
if (!assetRefs[i].Equals(other.assetRefs[i]))
return false;
}
for (int i = 0, c = soundDefs.Length; i < c; ++i)
{
if (!soundDefs[i].Equals(other.soundDefs[i]))
return false;
}
return true;
}
public override int GetHashCode()
{
if (assetRefs == null)
return 0;
int h = 0;
foreach (var a in assetRefs)
h ^= a.GetHashCode();
return h;
}
}