ScrollView自适应大小及屏幕DebugLogPanel

游戏开始接入各种平台,所以需要一个可以在屏幕上能看到的Debug面板. 制作方法如下

QQ20171204-170729

创建ScrollView组件,其中Content上面需要绑定Content Size FitterVertical Layout Group

然后再最外层挂载CELog代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class CELog : MonoBehaviour
{
private static CELog mInstance;

public static void Warring(string _msg)
{
Log(_msg, "yellow");
}

public static void Error(string _msg)
{
Log(_msg, "red");
}

public static void Log(string _msg)
{
mInstance.SetLog(_msg);
}

public static void Log(string _msg, string _color)
{
mInstance.SetLog("<color=" + _color + ">" + _msg + "</color>");
}

public Text logPanel;
public int maxLogNum = 100;

private string mLogMsg;
private int mCurrentLogNum;


private void Start()
{
mInstance = this;
mLogMsg = "";
logPanel.text = "";
}

private void SetLog(string _msg)
{
mCurrentLogNum++;
if (mCurrentLogNum >= maxLogNum)
{
mLogMsg = "";
}
mLogMsg += _msg + "\n";
logPanel.text = mLogMsg;
}
}

外部调用方法:

1
2
3
4
5
6
7
public void OnClickBtn1()
{
CELog.Log("This is normal message");
CELog.Warring("This is warring message");
CELog.Error("This is error message");
CELog.Log("This is cyan color message", "cyan");
}

执行结果:

QQ20171204-172816

ScrollView可以根据内部内容多少自适应大小

官方文档有相应的HowTo教程

坚持原创技术分享,您的支持将鼓励我继续创作!