UnitFactory.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using Unity.Mathematics;
  2. namespace ET.Client
  3. {
  4. public static class UnitFactory
  5. {
  6. public static Unit Create(Scene currentScene, UnitInfo unitInfo)
  7. {
  8. UnitComponent unitComponent = currentScene.GetComponent<UnitComponent>();
  9. Unit unit = unitComponent.AddChildWithId<Unit, int>(unitInfo.UnitId, unitInfo.ConfigId);
  10. unitComponent.Add(unit);
  11. unit.Position = unitInfo.Position;
  12. unit.Forward = unitInfo.Forward;
  13. NumericComponent numericComponent = unit.AddComponent<NumericComponent>();
  14. foreach (var kv in unitInfo.KV)
  15. {
  16. numericComponent.Set(kv.Key, kv.Value);
  17. }
  18. unit.AddComponent<MoveComponent>();
  19. if (unitInfo.MoveInfo != null)
  20. {
  21. if (unitInfo.MoveInfo.Points.Count > 0)
  22. {
  23. unitInfo.MoveInfo.Points[0] = unit.Position;
  24. unit.MoveToAsync(unitInfo.MoveInfo.Points).Coroutine();
  25. }
  26. }
  27. unit.AddComponent<ObjectWait>();
  28. unit.AddComponent<XunLuoPathComponent>();
  29. EventSystem.Instance.Publish(unit.DomainScene(), new EventType.AfterUnitCreate() {Unit = unit});
  30. return unit;
  31. }
  32. }
  33. }