public Entity CreateEntity(WeakAssetReference assetGuid) { var resource = GetSingleAssetResource(assetGuid); if (resource == null) { return(Entity.Null); } var prefab = resource as GameObject; if (prefab != null) { var gameObjectEntity = m_world.Spawn <GameObjectEntity>(prefab); return(gameObjectEntity.Entity); } var factory = resource as ReplicatedEntityFactory; if (factory != null) { return(factory.Create(m_world.GetEntityManager(), this, m_world)); } return(Entity.Null); }
public Entity CreateEntity(WeakAssetReference assetGuid, World ecsWorld = null) { var resource = GetSingleAssetResource(assetGuid); if (resource == null) { return(Entity.Null); } var prefab = resource as GameObject; if (prefab != null) { Entity ent; m_world.SpawnInternal(prefab, Vector3.zero, Quaternion.identity, out ent, ecsWorld); return(ent); } var factory = resource as ReplicatedEntityFactory; if (factory != null) { return(factory.Create(m_world.GetEntityManager(), this, m_world)); } return(Entity.Null); }
public float GetTransitionDuration(WeakAssetReference from, WeakAssetReference to) { var duration = DefaultTransitionDuration; for (int i = 0; i < TransitionDuration.Length; i++) { if (TransitionDuration[i].From != WeakAssetReference.Default) { if (TransitionDuration[i].From == from && TransitionDuration[i].To == to) { duration = TransitionDuration[i].Duration; break; } continue; } if (TransitionDuration[i].To == to) { duration = TransitionDuration[i].Duration; } } return(duration); }
public override void PrepareForBuild() { Debug.Log("ReplicatedEntityRegistry"); entries.Clear(); var guids = AssetDatabase.FindAssets("t:GameObject"); foreach (var guid in guids) { var path = AssetDatabase.GUIDToAssetPath(guid); var go = AssetDatabase.LoadAssetAtPath <GameObject>(path); var replicated = go.GetComponent <ReplicatedEntity>(); if (replicated == null) { continue; } //// PrefabUtility.LoadPrefabContents() // var stage = PrefabStageUtility.GetPrefabStage(go); // stage.prefabAssetPath replicated.SetAssetGUID(guid); Debug.Log(" Adding guid:" + guid + " prefab:" + path); var guidData = new WeakAssetReference(guid); entries.Add(new Entry { guid = guidData, prefab = new WeakAssetReference(guid) }); } guids = AssetDatabase.FindAssets("t:ReplicatedEntityFactory"); foreach (var guid in guids) { var path = AssetDatabase.GUIDToAssetPath(guid); var factory = AssetDatabase.LoadAssetAtPath <ReplicatedEntityFactory>(path); factory.SetAssetGUID(guid); Debug.Log(" Adding guid:" + guid + " factory:" + factory); var guidData = new WeakAssetReference(guid); entries.Add(new Entry { guid = guidData, factory = factory }); } EditorUtility.SetDirty(this); }
public Entry GetEntry(WeakAssetReference guid) { var index = GetEntryIndex(guid); if (index != -1) { return(entries[index]); } return(null); }
public void SetAssetGUID(string guidStr) { var newRef = new WeakAssetReference(guidStr); if (!newRef.Equals(guid)) { guid = newRef; EditorUtility.SetDirty(this); } }
public SoundDef GetSoundDef(WeakAssetReference assetRef) { if (assetRefs == null) { return(null); } var i = assetRefs.IndexOf(assetRef); return(i > -1 ? soundDefs[i] : null); }
public int GetEntryIndex(WeakAssetReference guid) { for (int i = 0; i < entries.Count; i++) { if (entries[i].guid.Equals(guid)) { return(i); } } return(-1); }
internal static BlobAssetReference <T> LoadAsset <T>(WeakAssetReference assetRef) where T : struct { var path = GetBlobAssetPath(assetRef.GetGuidStr()); if (!File.Exists(path)) { throw new FileNotFoundException("Cannot find file:" + path); } return(BlobAssetReference <T> .Create(File.ReadAllBytes(path))); }
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)); }
// public Object LoadSingleAssetResource(string guid) // { // if (guid == null || guid == "") // { // GameDebug.LogError("Guid invalid"); // return null; // } // // var reference = new WeakAssetReference(guid); // return GetSingleAssetResource(reference); // } public Object GetSingleAssetResource(WeakAssetReference reference) { var def = new SingleResourceBundle(); if (m_resources.TryGetValue(reference, out def)) { return(def.asset); } def = new SingleResourceBundle(); var useBundles = !Application.isEditor || forceBundles.IntValue > 0; var guidStr = reference.GetGuidStr(); #if UNITY_EDITOR if (!useBundles) { var path = AssetDatabase.GUIDToAssetPath(guidStr); def.asset = AssetDatabase.LoadAssetAtPath(path, typeof(UnityEngine.Object)); if (def.asset == null) { GameDebug.LogWarning("Failed to load resource " + guidStr + " at " + path); } if (verbose.IntValue > 0) { GameDebug.Log("resource: loading non-bundled asset " + path + "(" + guidStr + ")"); } } #endif if (useBundles) { var bundlePath = GetBundlePath(); def.bundle = AssetBundle.LoadFromFile(bundlePath + "/" + m_assetResourceFolder + "/" + guidStr); if (verbose.IntValue > 0) { GameDebug.Log("resource: loading bundled asset: " + m_assetResourceFolder + "/" + guidStr); } var handles = def.bundle.LoadAllAssets(); if (handles.Length > 0) { def.asset = handles[0]; } else { GameDebug.LogWarning("Failed to load resource " + guidStr); } } m_resources.Add(reference, def); return(def.asset); }
public static void Create(EntityCommandBuffer commandBuffer, WeakAssetReference assetGuid, Vector3 position, Vector3 velocity, Entity owner, int teamId) { var data = new GrenadeSpawnRequest(); data.assetGuid = assetGuid; data.position = position; data.velocity = velocity; data.owner = owner; data.teamId = teamId; commandBuffer.AddComponent(commandBuffer.CreateEntity(), data); }
public Entity CreateEntity(string guid) { if (guid == null || guid == "") { GameDebug.LogError("Guid invalid"); return(Entity.Null); } var reference = new WeakAssetReference(guid); return(CreateEntity(reference)); }
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 WeakAssetReference SelfReference; // TODO (mogensh) remove when all conversion is in editor (and we can use AssetDatabase) #if UNITY_EDITOR private void OnValidate() { var path = AssetDatabase.GetAssetPath(this); if (string.IsNullOrEmpty(path)) { return; } var guid = AssetDatabase.AssetPathToGUID(path); SelfReference = new WeakAssetReference(guid); }
public bool GetPresentation(WeakAssetReference ownerGuid, out WeakAssetReference presentationGuid) { foreach (var entry in m_entries) { if (entry.ownerAssetGuid == ownerGuid) { presentationGuid = entry.presentation; return(true); } } presentationGuid = new WeakAssetReference(); return(false); }
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); }
public bool SetAssetGUID(string guidStr) { var guid = new WeakAssetReference(guidStr); var val = Value; var currentGuid = val.assetGuid; if (!guid.Equals(currentGuid)) { val.assetGuid = guid; Value = val; PrefabUtility.SavePrefabAsset(gameObject); return(true); } return(false); }
public bool FindAsset(int channelId, int partId, int skeletonHash, int lod, ref WeakAssetReference result) { var rigFlag = 0xFFFF; if (skeletonHash != 0) { rigFlag = 0; for (int i = 0; i < Rigs.Length; i++) { if (Rigs[i].skeletonHash == skeletonHash) { rigFlag = 1 << i; break; } } } for (int i = 0; i < Entries.Length; i++) { var entry = Entries[i]; //GameDebug.Log($"{Entries.Length} entry: {entry.CategoryId} {entry.PartId} {entry.RigFlags}&{rigFlag} {entry.LODFlags}&{1 << lod}"); if (entry.CategoryId != channelId) { continue; } if (entry.PartId != partId) { continue; } if ((entry.RigFlags & rigFlag) == 0) { continue; } var lodFlag = 1 << lod; if ((entry.LODFlags & lodFlag) == 0) { continue; } result = entry.Asset; return(true); } return(false); }
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { var buffer = dstManager.AddBuffer <PrefabAssetRegistry.Entry>(entity); for (int i = 0; i < Assets.Count; i++) { var assetPath = AssetDatabase.GetAssetPath(Assets[i]); var assetGUID = AssetDatabase.AssetPathToGUID(assetPath); var reference = new WeakAssetReference(assetGUID); var prefabEntity = conversionSystem.GetPrimaryEntity(Assets[i]); buffer.Add(new PrefabAssetRegistry.Entry { Reference = reference, EntityPrefab = prefabEntity, }); } dstManager.AddComponentData(entity, new PrefabAssetRegistry.State()); }
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { var registry = PartRegistryAsset.GetComponent <PartRegistryAuthoring>(); while (PartIds.Count < registry.Categories.Count) { PartIds.Add(0); } var packedPartId = registry.PackPartsList(PartIds.ToArray()); var inputState = PartOwner.InputState.Default; inputState.PackedPartIds = packedPartId; dstManager.AddComponentData(entity, inputState); var registryPath = AssetDatabase.GetAssetPath(PartRegistryAsset); var registryGuid = AssetDatabase.AssetPathToGUID(registryPath); var registryRef = new WeakAssetReference(registryGuid); dstManager.AddComponentData(entity, new PartOwner.RegistryAsset(registryRef)); }
public static Entity FindEntityPrefab(EntityManager entityManager, WeakAssetReference assetGuid) { var entity = GetRegistryEntity(entityManager); if (entity == Entity.Null) { return(Entity.Null); } var entries = entityManager.GetBuffer <PrefabAssetRegistry.Entry>(entity); for (int i = 0; i < entries.Length; i++) { if (entries[i].Reference.Equals(assetGuid)) { return(entries[i].EntityPrefab); } } return(Entity.Null); }
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); }
public override void OnInspectorGUI() { var entry = target as PartRegistryAssetEntry; var registry = entry.GetComponentInParent <PartRegistryAuthoring>(); EditorGUI.BeginChangeCheck(); // TODO (mogensh) make general method to make WeakAssetReference field var guidStr = ""; if (entry.Asset.IsSet() && m_lastReference != entry.Asset) { guidStr = entry.Asset.ToGuidStr(); var path = AssetDatabase.GUIDToAssetPath(guidStr); var obj = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)); lastObject = obj; m_lastReference = entry.Asset; } GUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel(new GUIContent("Asset" + "(" + guidStr + ")")); var newObj = EditorGUILayout.ObjectField(lastObject, typeof(GameObject), false); GUILayout.EndHorizontal(); if (newObj != lastObject) { if (newObj != null) { var path = AssetDatabase.GetAssetPath(newObj); entry.Asset = new WeakAssetReference(AssetDatabase.AssetPathToGUID(path)); } } var catNames = new string[registry.Categories.Count]; for (var i = 0; i < registry.Categories.Count; i++) { catNames[i] = registry.Categories[i].Name; } entry.CategoryIndex = EditorGUILayout.Popup("Category", entry.CategoryIndex, catNames); var parts = registry.Categories[entry.CategoryIndex].Parts; var partNames = new string[parts.Count]; for (var i = 0; i < parts.Count; i++) { partNames[i] = parts[i].Name; } entry.PartIndex = EditorGUILayout.Popup("Part", entry.PartIndex, partNames); // Build type var buildTypeNames = Enum.GetNames(typeof(BuildType)); entry.BuildTypeFlags = EditorGUILayout.MaskField("BuildTypes", entry.BuildTypeFlags, buildTypeNames); // Rig if (registry.Rigs.Count > 1) { var rigNames = new string[registry.Rigs.Count]; for (var i = 0; i < registry.Rigs.Count; i++) { rigNames[i] = i + ":" + registry.Rigs[i]; } entry.RigFlags = EditorGUILayout.MaskField("Rig", entry.RigFlags, rigNames); } // LOD if (registry.LODlevels.Count > 1) { var LODNames = new string[registry.LODlevels.Count]; for (int i = 0; i < registry.LODlevels.Count; i++) { LODNames[i] = "LOD" + i; } entry.LODFlags = EditorGUILayout.MaskField("LOD", entry.LODFlags, LODNames); } var buildString = "Build:" + GetFlagString(entry.BuildTypeFlags, Enum.GetNames(typeof(BuildType)).Length); var rigString = registry.Rigs.Count > 1 ? "Rig:" + GetFlagString(entry.RigFlags, registry.Rigs.Count) : ""; var lodString = registry.LODlevels.Count > 1 ? "LOD:" + GetFlagString(entry.LODFlags, registry.LODlevels.Count) : ""; var partStr = parts[entry.PartIndex].Name; entry.name = registry.Categories[entry.CategoryIndex].Name + "." + partStr + "<" + buildString + " " + rigString + " " + lodString + ">"; var change = EditorGUI.EndChangeCheck(); if (change) { EditorUtility.SetDirty(entry); EditorUtility.SetDirty(entry.gameObject); EditorUtility.SetDirty(registry.gameObject); } }
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); }
public ReplicatedEntityData(WeakAssetReference guid) { this.assetGuid = guid; id = -1; predictingPlayerId = -1; }
public RegistryAsset(WeakAssetReference value) { Value = value; }
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); } }); }
public static BlobAssetReference <PartRegistry.PartRegistryBlob> GetPartRegistry(World world, WeakAssetReference asset) { var tuple = new Tuple <World, WeakAssetReference>(world, asset); var registry = BlobAssetReference <PartRegistry.PartRegistryBlob> .Null; if (g_PartRegistries.TryGetValue(tuple, out registry)) { return(registry); } var entity = PrefabAssetManager.CreateEntity(world.EntityManager, asset); var partRegistryData = world.EntityManager.GetComponentData <PartRegistryData>(entity); registry = partRegistryData.Value; // GameDebug.Log("Loaded registry"); g_PartRegistries.Add(tuple, registry); return(registry); }
public SoundSystem.SoundHandle Play(WeakAssetReference weakSoundDef, Vector3 position) { return(default(SoundSystem.SoundHandle)); }