public static GUnit CreateAsset(this UnitFactory factory, int requiredModuleGroup, GameObject asset, bool needSpawned = true) { var unit = WorldManager.Instance.Factory.CreateUnit(requiredModuleGroup); if (needSpawned) { asset.Spawn(unit.UnitId); } else { var assetData = asset.GetComponent <AssetData>(); if (assetData == null) { assetData = asset.AddComponent <AssetData>(); } unit.AddData(assetData); assetData.unitId = unit.UnitId; assetData.isSpawned = false; } var unitData = unit.GetData <UnitData>(); unitData.stateTypeProperty.Subscribe(_ => { if (_ == UnitStateType.Destroy) { var assetData = unit.GetData <AssetData>(); assetData.unitId = 0; if (assetData.isSpawned) { assetData.isSpawned = false; AssetProcess.Despawn(assetData.gameObject); } } }).AddTo(unitData.disposable); return(unit); }