98 lines
2.6 KiB
C#
98 lines
2.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System;
|
|
|
|
public class FunctionSync_Parent : OneValueSyncObject
|
|
{
|
|
/// <summary>
|
|
/// 是否为父物体
|
|
/// </summary>
|
|
public bool isParent;
|
|
|
|
private OneValueSyncObject localposSync;
|
|
private OneValueSyncObject localangSync;
|
|
|
|
private void Start()
|
|
{
|
|
Init();
|
|
}
|
|
|
|
public void Init()
|
|
{
|
|
if(!hasInit)
|
|
{
|
|
InitDynamic("parent_" + gameObject.name, CallBackParent, ValueType.String);
|
|
if (!isParent)
|
|
{
|
|
localposSync = gameObject.AddComponent<OneValueSyncObject>();
|
|
localposSync.InitDynamic(Id + "pos", CallBackPos, ValueType.Vector3);
|
|
localangSync = gameObject.AddComponent<OneValueSyncObject>();
|
|
localangSync.InitDynamic(Id + "ang", CallBackAng, ValueType.Vector3);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 设置父物体
|
|
/// </summary>
|
|
/// <param name="parent"></param>
|
|
/// <param name="Localpos"></param>
|
|
/// <param name="LocalEulerAngles"></param>
|
|
public void SetParent(FunctionSync_Parent parent, Vector3 Localpos,Vector3 LocalEulerAngles)
|
|
{
|
|
if(!isParent)
|
|
{
|
|
transform.parent = parent.transform;
|
|
transform.localPosition = Localpos;
|
|
transform.localEulerAngles = LocalEulerAngles;
|
|
|
|
mystring = parent.Id;
|
|
SendSync();
|
|
SetLocalPos(Localpos);
|
|
SetLocalAng(LocalEulerAngles);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("父物体不能再设置父物体");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 设置局部坐标
|
|
/// </summary>
|
|
public void SetLocalPos(Vector3 localpos)
|
|
{
|
|
localposSync.myvector3 = localpos;
|
|
localposSync.SendSync();
|
|
}
|
|
/// <summary>
|
|
/// 设置局部旋转
|
|
/// </summary>
|
|
public void SetLocalAng(Vector3 localang)
|
|
{
|
|
localangSync.myvector3 = localang;
|
|
localangSync.SendSync();
|
|
}
|
|
|
|
private void CallBackParent(string id, bool isEnterRoom)
|
|
{
|
|
if (!isParent)
|
|
{
|
|
transform.parent = OneValueSyncObject.OneAxisSyncObjectList[mystring].transform;
|
|
}
|
|
}
|
|
private void CallBackPos(string id, bool isEnterRoom)
|
|
{
|
|
if (!isParent)
|
|
{
|
|
transform.localPosition = localposSync.myvector3;
|
|
}
|
|
}
|
|
private void CallBackAng(string id, bool isEnterRoom)
|
|
{
|
|
if (!isParent)
|
|
{
|
|
transform.localEulerAngles = localangSync.myvector3;
|
|
}
|
|
}
|
|
}
|