讲完了 聊天消息了 下面说两个主界面 还有工具类
这里懒得再转****Sprite直接用RawImage了 他可以放Texture2D
登录界面 LoginPanel
登录界面.png应该没啥说的 就建这几个组件
代码:
using UnityEngine;
using UnityEngine.UI;
public class LoginPanel : MonoBehaviour
{
public InputField NameInput;
public RawImage PlayerHead;
public Button LoginBtn;
public Button SelectHeadBtn;
}
聊天界面ChatPanel
聊天界面.png每个聊天消息框都是由SpawnDialogUI这个函数进行创建的 创建完要下拉最下面消息框嘛 所以要更改Content的位置进行下拉操作
代码:
using UnityEngine;
using UnityEngine.UI;
public class ChatPanel : MonoBehaviour
{
public Button SendChatBtn;
public RawImage HeadImg;
public Text PlayerNameTxt;
public RectTransform DialogParent;
public DialogUI DialogUIPrefab;
public InputField chatInput;
/// <summary>
/// 生成对话框 懒得写枚举了。用int吧 0我方 其他对方
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public DialogUI SpawnDialogUI(string playername, string info,Texture2D head, int i=0)
{
DialogUI dialogUI;
if (i==0)
{
dialogUI = Instantiate(DialogUIPrefab, DialogParent.transform).InitLocalDialog(playername, info, head);
}
else
{
dialogUI = Instantiate(DialogUIPrefab, DialogParent.transform).InitDialog(playername, info, head);
}
//下拉对话框 上面应该会延时 所以这里也需要延时
Invoke("HaulDown",0.02f);
return dialogUI;
}
public void HaulDown()
{
DialogParent.anchoredPosition = new Vector2(DialogParent.anchoredPosition.x, DialogParent.sizeDelta.y);
}
}[/mw_shl_code]
**工具拓展类 ****ImageExtension**
ByteToTexture 就是Byte转成Texture2D图片
LoadImage 是RawImage 加载Byte[]数据的图片
**代码:**[mw_shl_code=csharp,false]using System.IO;
using UnityEngine;
using UnityEngine.UI;
public static class ImageExtension
{
public static void LoadImage(this RawImage rawImage,byte[] data,int Width=200,int Height=200)
{
Texture2D texture2D = new Texture2D(Width, Height);
texture2D.LoadImage(data);
rawImage.texture = texture2D;
}
public static Texture2D ByteToTexture(this byte[] data, int Width = 200, int Height = 200)
{
Texture2D texture2D = new Texture2D(Width, Height);
texture2D.LoadImage(data);
return texture2D;
}
/// <summary>
/// 路劲图片文件转byte[]
/// </summary>
/// <param name="filename填写图片路径"></param>
/// <returns></returns>
public static byte[] GetPictureBytes(this string filename)
{
FileInfo fileInfo = new FileInfo(filename);
byte[] buffer = new byte[fileInfo.Length];
using (FileStream stream = fileInfo.OpenRead())
{
stream.Read(buffer, 0, buffer.Length);
}
return buffer;
}
}
好了我们下篇最后一篇继续 基本功能都写在了GameRoot那 =。=
https://github.com/LKaiGuo/KGScriptGenerator 喜欢的给我点个星星啊
u3d萌新QQ群844087555 欢迎进来灌水=。=
网友评论