使用UGUI遇到的坑:Text组件无法首行缩进两格
其实只需要把输入法切换至全角
再输入空格即可缩进。
UGUI Text换行问题
有人说UGUI的Text不能换行,有人说可以通过 \n 换行,并附上了整条字符串。
其实他们根本就不在一个频道!!
这么说吧,通过代码直接给Text组件的text赋值" <color=red>XXXX</color>\nXXXX "绝对是可以换行效果的;然而,在Inspector面板的Text组件里输入同样的内容就不行,哪怕手拙复制进去都不对,这尼玛什么鬼!?
后来发现,原来它把\n偷偷变成了\\n了,所以我们只要把它变回来就行啦!
using UnityEngine;
using UnityEngine.UI;
public class RyanTextLineFeed : MonoBehaviour
{
Text myText;
void Start ()
{
myText = GetComponent<Text> ();
myText.text = myText.text.Replace ("\\n", "\n");
}
}
UGUI坐标的正确设置
UGUI坐标的正确设置方式是使用anchoredPosition这个属性,与编辑器看到的数值是一致的,不能用LocalPosition这个属性,localPosition这个属性设置的数值是针对锚点在中心的。
go.GetComponent<RectTransform>().anchoredPosition = new Vector3(-55, 0, 0);
加载本地图片:
图片的路径不能同时包含 "/" 和 "\",这样的路径是加载不成功的,如以下路径是错误的:
@"I:\UnityPlugin\superScrollview2.2.2\Archive/Lixi/codeScene/";
本地图片的路径分隔符 不管是 "/"还是 "\\" 都可以在路径前面加:"file://" 加载出图片来,如一下路径:
string str = "I:\\UnityPlugin\\superScrollview2.2.2\\Archive\\Lixi\\codeScene\\";
string str3 = @"I:\UnityPlugin\superScrollview2.2.2\Archive\Lixi\codeScene\";
string str2 = "I:/UnityPlugin/superScrollview2.2.2/Archive/Lixi/codeScene/";
在这里遇到一个坑,那就是如果图片的名称过长,是没法通过www加载出来的,如以下两张图片:解决方法是通过IO加载
万全加载本地图片的方式如下:
IEnumerator getPicFromLocal(string filePath, Action<Texture> callBack)
{
string addFilePrefix = "file://" + LocalPathCorrect(filePath);
WWW www = new WWW(addFilePrefix);
while (!www.isDone)
{
yield return new WaitForEndOfFrame();
}
if (!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error+ addFilePrefix);
callBack(readLocalTexture2d(filePath));
}
else
{
if (callBack != null)
{
callBack(www.texture);
Debug.Log("图片本地加载成功:" + filePath);
}
}
www.Dispose();
}
public static Texture2D readLocalTexture2d(string filePath)
{
if (!File.Exists(filePath))
return null;
Texture2D t2d = null;
try
{
//读取文件
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
int byteLength = (int)fs.Length;
byte[] imgBytes = new byte[byteLength];
fs.Read(imgBytes, 0, byteLength);
fs.Close();
fs.Dispose();
//转化为Texture2D
t2d = new Texture2D(1024, 1024);
t2d.LoadImage(imgBytes);
t2d.Apply();
return t2d;
}
catch { }
return t2d;
}
/// <summary>
/// 纠正路径,以防出现"/"和 "\"
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string LocalPathCorrect(string url)
{
string path = url;
path = path.Replace("//", "/");
path = path.Replace("\\", "/");
path = path.Replace("\\\\", "/");
path = path.Replace('\\', '/');
path = path.Replace('/', '/');
return path;
}
1.以下是绑定按钮点击事件响应,每次点击按钮时会出现数组越界问题,输出的i为2,解决方案是for循环换成foreach
void SetBtnHandle()
{
for (int i = 0; i < buildAndDelList.Count; i++)
{
buildAndDelList[i].onClick.AddListener(() => {
Debug.Log(i);
OnBtnClick(buildAndDelList[i].transform.name);
});
Debug.Log(buildAndDelList[i].name);
}
foreach(var to in btnGroupList)
{
to.onClick.AddListener(()=> { OnBtnGroup(to.gameObject.name); });
}
}
void OnBtnClick(string name)
{
foreach (var btn in buildAndDelList)
{
if (btn.name.Equals(name))
{
btn.transform.GetChild(0).GetComponent<Image>().color = new Color(1f, 1f, 1f, 1f);
}
else
{
btn.transform.GetChild(0).GetComponent<Image>().color = new Color(1f, 1f, 1f, 0f);
}
}
PcValue.buildScene_isBuild = (name == "buildtBtn" ? true : false);
}