69 lines
1.6 KiB
C#
69 lines
1.6 KiB
C#
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);
|
||
}
|
||
}
|
||
}
|
||
}
|