Advertisement
_EagleOwle_

UnityNetEntity

Aug 22nd, 2020
1,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1. using GalaxyCoreLib;
  2. using GalaxyCoreLib.NetEntity;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using UnityEngine;
  8.  
  9. public class UnityNetEntity : MonoBehaviour
  10. {
  11.     /// <summary>
  12.     /// Ссылка на сетевую сущность в ядре
  13.     /// </summary>
  14.     public ClientNetEntity netEntity = new ClientNetEntity();
  15.     /// <summary>
  16.     /// буфер дополнительный данных который можно приложить при инициализации
  17.     /// </summary>
  18.     [HideInInspector]
  19.     public byte[] data = null;
  20.     /// <summary>
  21.     /// Время инициализации объекта
  22.     /// </summary>
  23.     [HideInInspector]
  24.     public float initTime;
  25.    
  26.     private Component[] components;
  27.  
  28.     void Awake()
  29.     {
  30.         netEntity.OnNetStart += OnNetStart;
  31.         netEntity.OnNetDestroy += OnNetDestroy;
  32.     }
  33.  
  34.     void Start()
  35.     {
  36.         components = GetComponents<Component>();
  37.         foreach (var item in components)
  38.         {
  39.             netEntity.galaxyVars.RegistrationClass(item);
  40.         }
  41.         // Init необходимо вызывать именно в Start т.к внутренняя инициализация не успевает сработать к Awake или OnEnable
  42.         Init();      
  43.     }
  44.  
  45.     void Init()
  46.     {
  47.      //   netEntity.OnNetStart += OnNetStart;
  48.      //   netEntity.OnNetDestroy += OnNetDestroy;
  49.        
  50.         if (!netEntity.isInit)
  51.         {
  52.             // если netEntity все еще null то это явно наш объект
  53.             // и нужно отправлять запрос на создание сетевого экземпляра
  54.             // создаем пустой экземпляр сетевой сущности
  55.           //  netEntity = new ClientNetEntity();            
  56.             // убираем все лишнее из имени
  57.             netEntity.prefabName = gameObject.name.Split(new char[] { ' ', '(' })[0];
  58.             // записываем текущую позицию и поворот
  59.             netEntity.transform.position = transform.position.NetworkVector3();
  60.             netEntity.transform.rotation = transform.rotation.NetworkQuaternion();
  61.             // отправляем запрос на создание сетевого объекта
  62.             GalaxyApi.netEntity.Instantiate(netEntity);                  
  63.         }              
  64.     }
  65.  
  66.     private void OnNetStart()
  67.     {
  68.      //   Debug.Log(gameObject.name + " OnNetStart");
  69.         //задаем время инициализации
  70.         initTime = Time.time;
  71.     }
  72.  
  73.     private void OnNetDestroy()
  74.     {
  75.         //Удаляем объект согласно команде сервера
  76.         Destroy(gameObject);
  77.     }
  78.  
  79.     private void OnDestroy()
  80.     {
  81.         netEntity.OnNetStart -= OnNetStart;
  82.         netEntity.OnNetDestroy -= OnNetDestroy;
  83.         // если же мы удаляем объект по своей инициативе то сообщяем об этом сетевой сущности
  84.         if (netEntity!=null) netEntity.Destroy();
  85.     }
  86.  
  87.     void LoadRPC()
  88.     {
  89.         List<MemberInfo> options_rpc = new List<MemberInfo>();
  90.         options_rpc = components.GetType().GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
  91.             .Where(n => Attribute.IsDefined(n, typeof(NetEntityRPC))).ToList();
  92.     }
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement