UGUI不规则形状按钮 各种技巧
using UnityEngine;
using System;
using System.Collections;
using UnityEngine.UI;
public class RaycastFilter : MonoBehaviour, ICanvasRaycastFilter
{
private RectTransform rectTransform;
private Image image;
public void Awake()
{
collider2D = GetComponent<BoxCollider2D>();
image = GetComponent<Image>();
}
public bool IsRaycastLocationValid(Vector2 screenPosition, Camera raycastEventCamera) //uGUI callback
{
if (image == null)
return true;
Vector2 localPoint;
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, screenPosition, raycastEventCamera, out localPoint);
var normalized = new Vector2(
(localPoint.x + rectTransform.pivot.x*rectTransform.rect.width)/rectTransform.rect.width,
(localPoint.y + rectTransform.pivot.y*rectTransform.rect.height)/rectTransform.rect.width);
Rect rect = image.sprite.textureRect;
var x = Mathf.FloorToInt(rect.x + rect.width * normalized.x);
var y = Mathf.FloorToInt(rect.y + rect.height * normalized.y);
try
{
return image.sprite.texture.GetPixel(x,y).a > 0.2f;
}
catch (UnityException e)
{
Debug.LogError("Mask texture not readable, set your sprite to Texture Type 'Advanced' and check 'Read/Write Enabled'");
Destroy(this);
return false;
}
}
}标签: uGUI
制作UGUI美术字体 Unity游戏研究
制作美术字体的教程网上有很多了,我借鉴了网上众多大神的方法, 在此感谢他们。
额外工具:BMFont 下载
先用BMFont编辑美术字,并导出文本信息和图片。
PS:导出图片的大小适合你的美术图片集就好,不要有太大浪费。
导出的文件
有了这两个文件就可以在unity里做美术字体了。
先把这两个文件导入unity工程,然后再创建一个customFont和一个材质球。
然后就是读取数据文件了,这里使用了NGUI里的脚本BMFontReader等
再用脚本读取数据并赋到customFont里,代码
using UnityEngine;
using System.Collections;
using UnityEditor;
public class MyFont : Editor
{
[MenuItem("MakeFont/Make")]
static void Make()
{
Object[] objs = Selection.objects;
if (objs.Length != 2)
{
Debug.LogError("请选中Custom Font文件与BMFont导出的fnt数据文件");
return;
}
Font m_myFont;
TextAsset m_data;
if (objs[0].GetType() == typeof(TextAsset)&&objs[1].GetType()==typeof(Font))
{
Debug.Log("text");
m_data = (TextAsset)objs[0];
m_myFont = (Font)objs[1];
Debug.Log("FontName:" + m_myFont.name + "\nData:" + m_data.name);
}
else if (objs[1].GetType() == typeof(TextAsset) && objs[0].GetType() == typeof(Font))
{
m_data = (TextAsset)objs[1];
m_myFont = (Font)objs[0];
Debug.Log("FontName:"+m_myFont.name+"\nData:"+m_data.name);
}
else
{
Debug.LogError("请选中Custom Font文件与BMFont导出的fnt数据文件");
Debug.Log("FontName:null" + "\nData:null");
return;
}
BMFont mbFont = new BMFont();
BMFontReader.Load(mbFont, m_data.name, m_data.bytes); // 借用NGUI封装的读取类
CharacterInfo[] characterInfo = new CharacterInfo[mbFont.glyphs.Count];
for (int i = 0; i < mbFont.glyphs.Count; i++)
{
BMGlyph bmInfo = mbFont.glyphs[i];
CharacterInfo info = new CharacterInfo();
info.index = bmInfo.index;
info.uv.x = (float)bmInfo.x / (float)mbFont.texWidth;
info.uv.y =1- (float)bmInfo.y / (float)mbFont.texHeight;
info.uv.width = (float)bmInfo.width / (float)mbFont.texWidth;
info.uv.height =- (float)bmInfo.height / (float)mbFont.texHeight;
info.vert.x = (float)bmInfo.offsetX;
info.vert.y = (float)bmInfo.offsetY;
info.vert.width = (float)bmInfo.width;
info.vert.height = (float)bmInfo.height;
info.width = (float)bmInfo.advance;
characterInfo[i] = info;
}
m_myFont.characterInfo = characterInfo;
Debug.Log("OK");
}
}
操作方法:选中customFont文件和导出的数据文件,再点击菜单 MakeFont>>Make,这样就把数据绑定了。
接下来就是把图片丢到材质球上,再把材质球丢到customFont的Default Material里(也可以不丢)。
然后创建一个Text看下效果,
PS:如果字体重叠了,可以把Text的width加大。下面是图片和材质等的设置,可参考。
这个shader是拿的国外一个收费插件的,花了我13刀!可以向我要。
字体文件包括customFont,材质,图片。
uGUI长按事件 Unity游戏研究
突然发现uGUI没有给出可以直接用的长按事件。。。
上个没有注释的代码,代码说明一切。
using UnityEngine;
using System.Collections;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class UILongPressEvent : MonoBehaviour,IPointerDownHandler,IPointerExitHandler,IPointerUpHandler
{
[SerializeField]
UnityEvent m_onLongPress = new UnityEvent();
float interval = 0.1f;
float longPressDelay = 0.5f;
private bool isTouchDown = false;
private bool isLongpress = false;
private float touchBegin = 0;
private float lastInvokeTime = 0;
// Update is called once per frame
void Update ()
{
if (isTouchDown)
{
if (isLongpress)
{
if (Time.time - lastInvokeTime > interval)
{
m_onLongPress.Invoke();
lastInvokeTime = Time.time;
}
}
else
{
if (Time.time - touchBegin > longPressDelay)
{
isLongpress = true;
}
}
}
}
public void OnPointerDown(PointerEventData eventData)
{
touchBegin = Time.time;
isTouchDown = true;
}
public void OnPointerExit(PointerEventData eventData)
{
isTouchDown = false;
isLongpress = false;
}
public void OnPointerUp(PointerEventData eventData)
{
isTouchDown = false;
isLongpress = false;
}
}










