Пример #1
0
 public static void RegenerateAllGhosts(GhostCollectionAuthoringComponent collectionTarget)
 {
     foreach (var ghost in collectionTarget.Ghosts)
     {
         if (ghost.prefab == null || !ghost.enabled)
         {
             continue;
         }
         GhostAuthoringComponentEditor.SyncComponentList(ghost.prefab);
         GhostAuthoringComponentEditor.GenerateGhost(ghost.prefab);
     }
 }
        public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
        {
            var conversionTarget = GhostCollectionAuthoringComponent.GetConversionTarget(dstManager.World);

            var mgr = dstManager.World.GetOrCreateSystem <GameObjectManager>();

            mgr.InitPrefabs(_authoring.ghostCollectionConfig.Prefabs.Count);
            for (int i = 0; i < _config.Prefabs.Count; i++)
            {
                mgr.AddPrefab(i, _config.Prefabs[i]);
            }

            Transform hidden = new GameObject().transform;

            hidden.gameObject.SetActive(false);
            hidden.name = $"__{conversionTarget}__";
            // Ghost处理
            GhostCollectionConfig.Ghost[] ghosts = _authoring.GetGhosts();
            mgr.InitGhosts(ghosts.Length);
            for (int i = 0; i < ghosts.Length; i++)
            {
                var item       = ghosts[i];
                var originType = item.prefab.Type;

                GameObject tmpServer = Instantiate(item.prefab.gameObject, hidden);
                tmpServer.name = $"{item.prefab.name}[SERVER]";
                RemoveComponent(tmpServer, TargetWorld.Server);
                mgr.AddGhostToServer(i, tmpServer);

                if (conversionTarget != TargetWorld.Client)
                {
                    continue;
                }

                item.prefab.Type = GhostAuthoringComponent.ClientInstanceType.Predicted;
                GameObject tmpPrediction = Instantiate(item.prefab.gameObject, hidden);
                tmpPrediction.name = $"{item.prefab.name}[CP]";
                RemoveComponent(tmpPrediction, conversionTarget);
                mgr.AddGhostToPredicted(i, tmpPrediction);

                item.prefab.Type = GhostAuthoringComponent.ClientInstanceType.Interpolated;
                GameObject tmpInterpolated = Instantiate(item.prefab.gameObject, hidden);
                tmpInterpolated.name = $"{item.prefab.name}[CI]";
                RemoveComponent(tmpInterpolated, conversionTarget);
                mgr.AddGhostToInterpolated(i, tmpInterpolated);

                item.prefab.Type = originType;
            }
        }
Пример #3
0
        public static void AddAllNewGhosts(GhostCollectionAuthoringComponent collectionTarget)
        {
            var  list         = collectionTarget.Ghosts;
            var  alreadyAdded = new HashSet <GhostAuthoringComponent>();
            bool hasEmpty     = false;

            foreach (var ghost in list)
            {
                if (ghost.prefab != null)
                {
                    alreadyAdded.Add(ghost.prefab);
                }
                else
                {
                    hasEmpty = true;
                }
            }

            if (hasEmpty)
            {
                for (int i = 0; i < list.Count; ++i)
                {
                    if (list[i].prefab == null)
                    {
                        list.RemoveAt(i);
                        --i;
                        EditorUtility.SetDirty(collectionTarget);
                    }
                }
            }

            var prefabGuids = AssetDatabase.FindAssets("t:" + typeof(GameObject).Name);

            foreach (var guid in prefabGuids)
            {
                var path  = AssetDatabase.GUIDToAssetPath(guid);
                var go    = AssetDatabase.LoadAssetAtPath <GameObject>(path);
                var ghost = go.GetComponent <GhostAuthoringComponent>();
                if (ghost != null && !alreadyAdded.Contains(ghost))
                {
                    list.Add(new GhostCollectionAuthoringComponent.Ghost {
                        prefab = ghost, enabled = true
                    });
                    EditorUtility.SetDirty(collectionTarget);
                }
            }
        }
Пример #4
0
        public static GhostCodeGen.Status GenerateCollection(GhostCollectionAuthoringComponent collectionTarget, bool testOnly = false)
        {
            var serializerCodeGen   = new GhostCodeGen("Packages/com.unity.netcode/Editor/CodeGenTemplates/GhostSerializerCollection.cs");
            var deserializerCodeGen = new GhostCodeGen("Packages/com.unity.netcode/Editor/CodeGenTemplates/GhostDeserializerCollection.cs");

            var assetPath = GhostCodeGen.GetPrefabAssetPath(collectionTarget.gameObject);

            int ghostCount = 0;
            var namePrefix = collectionTarget.NamePrefix;

            var localReplacements = new Dictionary <string, string>();

            for (int i = 0; i < collectionTarget.Ghosts.Count; ++i)
            {
                var ghost = collectionTarget.Ghosts[i];
                if (ghost.prefab != null && ghost.enabled)
                {
                    ++ghostCount;
                    var serializerTypeName = ghost.prefab.name + "GhostSerializer";
                    var snapshotTypeName   = ghost.prefab.name + "SnapshotData";
                    var spawnerTypeName    = ghost.prefab.name + "GhostSpawnSystem";

                    localReplacements.Clear();
                    localReplacements.Add("GHOST_SERIALIZER_TYPE", serializerTypeName);
                    localReplacements.Add("GHOST_SNAPSHOT_TYPE", snapshotTypeName);
                    localReplacements.Add("GHOST_SPAWNER_TYPE", spawnerTypeName);
                    localReplacements.Add("GHOST_SERIALIZER_INDEX", i.ToString());
                    localReplacements.Add("GHOST_COLLECTION_PREFIX", namePrefix);

                    serializerCodeGen.GenerateFragment("GHOST_SERIALIZER_INSTANCE", localReplacements);
                    deserializerCodeGen.GenerateFragment("GHOST_DESERIALIZER_INSTANCE", localReplacements);

                    serializerCodeGen.GenerateFragment("GHOST_SERIALIZER_NAME", localReplacements);
                    serializerCodeGen.GenerateFragment("GHOST_FIND_TYPE", localReplacements);
                    serializerCodeGen.GenerateFragment("GHOST_BEGIN_SERIALIZE", localReplacements);
                    serializerCodeGen.GenerateFragment("GHOST_CALCULATE_IMPORTANCE", localReplacements);
                    serializerCodeGen.GenerateFragment("GHOST_SNAPSHOT_SIZE", localReplacements);
                    serializerCodeGen.GenerateFragment("GHOST_INVOKE_SERIALIZE", localReplacements);

                    deserializerCodeGen.GenerateFragment("GHOST_SERIALIZER_NAME", localReplacements);
                    deserializerCodeGen.GenerateFragment("GHOST_INITIALIZE_DESERIALIZE", localReplacements);
                    deserializerCodeGen.GenerateFragment("GHOST_BEGIN_DESERIALIZE", localReplacements);
                    deserializerCodeGen.GenerateFragment("GHOST_INVOKE_DESERIALIZE", localReplacements);
                    deserializerCodeGen.GenerateFragment("GHOST_INVOKE_SPAWN", localReplacements);
                }
            }

            var replacements = new Dictionary <string, string>();

            replacements.Add("GHOST_COLLECTION_PREFIX", namePrefix);
            replacements.Add("GHOST_SYSTEM_PREFIX", namePrefix);
            replacements.Add("GHOST_SERIALIZER_COUNT", ghostCount.ToString());
            var batch = new GhostCodeGen.Batch();

            serializerCodeGen.GenerateFile(assetPath, "", collectionTarget.SerializerCollectionPath, replacements, batch);
            deserializerCodeGen.GenerateFile(assetPath, "", collectionTarget.DeserializerCollectionPath, replacements, batch);
            bool didWrite = batch.Flush(testOnly);

            AssetDatabase.Refresh();
            return(didWrite ? GhostCodeGen.Status.Ok : GhostCodeGen.Status.NotModified);
        }
 private void Awake()
 {
     _authoring = GetComponent <GhostCollectionAuthoringComponent>();
     _config    = _authoring.ghostCollectionConfig;
 }