Unity3D: 自制提示框消息 (Toast)
根据 字符串长度 自适配窗体大小,绑定当前UI父节点:
// arg0: 提示信息
// arg1: 提示框体父节点
public void ShowToastUI(string str, Transform trans)
{
GameObject toast = Resources<span class="hljs-preprocessor">.Load</span>(<span class="hljs-string">"XXX"</span>) as GameObject<span class="hljs-comment">; // 加载预制体</span>
GameObject m_toast = Instantiate(toast, null, true)<span class="hljs-comment">; // 对象初始化</span>
m_toast<span class="hljs-preprocessor">.transform</span><span class="hljs-preprocessor">.parent</span> = trans<span class="hljs-comment">;</span>
m_toast<span class="hljs-preprocessor">.transform</span><span class="hljs-preprocessor">.localScale</span> = Vector3<span class="hljs-preprocessor">.one</span><span class="hljs-comment">;</span>
m_toast<span class="hljs-preprocessor">.transform</span><span class="hljs-preprocessor">.localPosition</span> = Vector3<span class="hljs-preprocessor">.zero</span><span class="hljs-comment">;</span>
RectTransform transform = m_toast<span class="hljs-preprocessor">.transform</span><span class="hljs-preprocessor">.GetComponent</span>()<span class="hljs-comment">;</span>
Text tips = m_toast<span class="hljs-preprocessor">.transform</span><span class="hljs-preprocessor">.FindChild</span>(<span class="hljs-string">"lab_toast"</span>)<span class="hljs-preprocessor">.GetComponent</span>()<span class="hljs-comment">;</span>
tips<span class="hljs-preprocessor">.text</span> = str<span class="hljs-comment">; </span>
// 根据字符长度,适配窗体大小
// arg0: 字符串长度*文本字体大小,再加上边距为提示窗体的宽度
// arg1: 提示窗体高度 <span class="hljs-number">45</span> (可根据自己需求做适当修改)
transform<span class="hljs-preprocessor">.sizeDelta</span> = new Vector2(str<span class="hljs-preprocessor">.Length</span> * tips<span class="hljs-preprocessor">.fontSize</span> + <span class="hljs-number">35</span>, <span class="hljs-number">45</span>)<span class="hljs-comment">;</span>
GameObject<span class="hljs-preprocessor">.Destroy</span>(m_toast, <span class="hljs-number">2</span>)<span class="hljs-comment">;</span>
}
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · unity3d mysql error: The given key was not present in the dictionary.
- · unity3d异步加载场景
- · unity3d Vector3.Lerp解析
- · unity3D导出模块给android使用
- · 曲线(轨迹)相似度算法——LCSS最长公共子序列算法
- · python版DTW动态时间规划算法
- · Unity中将3D模型显示在UI上或者显示在UI前面
- · 3dmax模型导入unity后很昏暗,对比度低怎么办?
- · unity中的简单延时方法
- · Unity导出apk时报错:UnauthorizedAccessException:Access to the path“F:\“ is denied
- · unity发布后无法连接sqlite数据库,在Editor中可以
- · 记录untiy适配android12