NewN_UAVPlane/Assets/Zion/Scripts/Sync1/FunctionSync_Text.cs

69 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
public class FunctionSync_Text : OneValueSyncObject
{
/// <summary>
/// 是否自动初始化,不可中途改变
/// </summary>
public bool isAutoInit=true;
Action<string, string> callback;
Text Text;
public void Init(Action<string, string> callback =null)
{
if(isAutoInit)
{
Text = GetComponent<Text>();
if(Text==null)
{
Debug.LogError("错误自动初始化需找到Text组件");
return;
}
}
else
{
if (callback != null)
{
this.callback = callback;
}
else
{
Debug.LogError("错误,自己初始化需要传回调");
return;
}
}
InitDynamic("text_" + gameObject.name, CallBack, ValueType.String);
}
/// <summary>
/// 如果是自动初始化,设置并同步字符串
/// </summary>
/// <param name="message"></param>
public void SetText(string message)
{
mystring = message;
if(callback!=null)
{
callback.Invoke(Id, message);
}
SendSync();
}
private void CallBack(string id, bool isEnterRoom)
{
if(isAutoInit)
{
Text.text = mystring;
}
else
{
if (callback != null)
{
callback(id, mystring);
}
}
}
}