刚拿到这个任务的时候我对Unity一无所知,想了好多方法,比如调用百度API、用GL绘制矩形框什么的,但是都不行,最后经由师兄指点知道了可以用GUI模块实现。写下这篇博客来纪念一下。 下图是最后的结果。
这个操作其实也蛮简单的,将一张图片放到Assets文件夹下,再创建一个C#脚本,就可以了。注意:这个脚本要拖到一个物体下才能执行,要有附着。创建的C#脚本名字要和类名相同。图片的类型要改为Sprite( 2D and UI)。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIMeshLine : Graphic
{
public Texture BoxTexture; // Drag a Texture onto this item in the Inspector
GUIContent content;
void Start()
{
content = new GUIContent("head", BoxTexture);
}
void OnGUI()
{
GUI.Box(new Rect(690, 300, 50, 50), content);
}
}
具体的GUI功能,各位看官可以去搜搜,有很多教程的。