首页 > 代码库 > UGUI代码分析

UGUI代码分析

1.canvas,screen模式和world模式区别:screen的绑定摄像机与canvas相对位置不会改变不可改变canvas,world模式下有个event camera相对位置可改变,canvas可改变,canvas就相当于一个3D物体。sorting,order layer规定canvas优先级。

2.富文本支持html标签。

3.控制image fill

public class imagetype : MonoBehaviour {
    public Image imagel;
    public float i = 0.0f;
}
void Update () {
        imagel.type = Image.Type.Filled;
        imagel.fillMethod = Image.FillMethod.Radial360;
        imagel.fillAmount = i;
        if (i >= 1.0f) { i = 0.0f; }
        i = i + 0.002f;
		
	}

4.控制按钮

public class button : MonoBehaviour {

    public Image image_sp;
    public Text text_sp;
    public Text text_btn;

    public void open_close(){
        if(text_btn.text == "打开图片"){
            image_sp.gameObject.SetActive(true);
            text_btn.text = "关闭图片";
            text_sp.text = "无敌是多么寂寞";
        }
        else {
            image_sp.gameObject.SetActive(false);
            text_btn.text = "打开图片";
            text_sp.text = "无敌?";
        }

5.控制按钮,内建图片数组

public class button1 : MonoBehaviour { 

    public Image image_sp;
    public Sprite[] image_sps;
    private string select_name;
    private int i;
    public void sp()
    {
        select_name = EventSystem.current.currentSelectedGameObject.name;
        if (select_name == "Button_left")
        {
            if (i <= 0) { i = image_sps.Length - 1; }
            image_sp.overrideSprite = image_sps[--i];
        }
        else
        {
            if (i >= image_sps.Length - 1) { i = 0; }
            image_sp.overrideSprite = image_sps[++i];
        }

6.锚点必须在他的父对象内设置,如果没有父对象就没有锚点。

7.InputField。Navigation自动匹配发布导航信息。

public class input_js : MonoBehaviour {
    public Text text_jg;
    public InputField input1;
    public InputField input2;
    public InputField input_to;
    public GameObject panel_tc;

    public void js(){
        text_jg.text = (int.Parse(input1.text) - int.Parse(input2.text)).ToString();
    }
    public void tc()
    {
        panel_tc.SetActive(true);
    }
    public void yes()
    {
        text_jg.text = input_to.text;
        panel_tc.SetActive(false);
    }
    public void no()
    {
        
        panel_tc.SetActive(false);
    }

}

8.xml本地存储登录案例技术分享

using System.IO;
using System.Xml;
public class dengl : MonoBehaviour
{
    public Image image_zc;
    public Text text_info;
    public Image image_ty_xxk;
    public Text text_ts;
    public InputField inputField_yhzh;
    public InputField inputField_yhmm;
    public InputField inputField_qrmm;
    public InputField inputField_yhdh;
    public InputField inputField_yhqq;
    public InputField inputField_zh;
    public InputField inputField_mm;
    private string _xmlpath;//yong hu wen jian,_xmlpath = Application.dataPath + "/user.xml";
    //获得date路径只能在主线程赋值,不能再moonbehaviour构造函数赋值如上
   /* public void text()
    {
        ty_xxk("用户名或密码不能为空");
    }*/
    void Start()
    {
        _xmlpath = Application.dataPath + "/user.xml";
        if (!File.Exists(_xmlpath))
        {
            XmlDocument xmlDoc = new XmlDocument();//xin jian xml shi li
            XmlElement root = xmlDoc.CreateElement("Root");//gen jie dian , zui shang ceng jie dian
            xmlDoc.AppendChild(root);//zhi jie dian
            XmlElement user = xmlDoc.CreateElement("User");//yong hu jie dian
            user.SetAttribute("user_name", "Admin");
            user.SetAttribute("user_pass", "123456");
            user.SetAttribute("user_tel", "18373290290");
            user.SetAttribute("user_qq", "1780585054");
            root.AppendChild(user);
            xmlDoc.Save(_xmlpath);
            Debug.Log("xml creat success");
        }
    }
    public void Quit()
    {
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
#else
        Application.Quit();
#endif
    }
    public void open_zc()
    {
        image_zc.gameObject.SetActive(true);
    }
    public void close_zc()
    {
        image_zc.gameObject.SetActive(false);
    }
    public void ty_xxk(string str)
    {
        image_ty_xxk.gameObject.SetActive(true);
        text_info.text = str;
        text_ts.text = str;
    }
    public void ty_xxk_close(string str)
    {
        image_ty_xxk.gameObject.SetActive(false);

    }
    public void zc()
    {
        if(inputField_yhzh.text == ""|| inputField_yhmm.text == "" || inputField_qrmm.text == "" || inputField_yhdh.text == "" || inputField_yhqq.text == ""){
            ty_xxk("信息不完整!有空的项目!");
            return;
        }
        if (inputField_yhmm.text != inputField_qrmm.text)
        {
            ty_xxk("两次输入的密码不一致");
            return;
        }
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(_xmlpath);
        XmlNodeList nodeList = xmlDoc.SelectSingleNode("Root").ChildNodes;
        foreach (XmlElement xe in nodeList)
        {
            if (xe.GetAttribute("user_name") == inputField_yhzh.text)
                ty_xxk("该用户名已被占用!请更换用户名!");
            
        }
        //ty_xxk("恭喜该用户名可以使用");
        //qu de gen jie dian
        XmlNode root = xmlDoc.SelectSingleNode("Root");
        //chuang jian xin jie dian
        XmlElement user = xmlDoc.CreateElement("User");
        user.SetAttribute("user_name", inputField_yhzh.text);
        user.SetAttribute("user_pass", inputField_yhmm.text);
        user.SetAttribute("user_tel", inputField_yhdh.text);
        user.SetAttribute("user_qq", inputField_yhqq.text);
        //jiang xin jie dian jia ru gen jie dian
        root.AppendChild(user);
        //bao cun wen jian
        xmlDoc.Save(_xmlpath);
        ty_xxk("恭喜注册成功");
        inputField_yhzh.text = "";
        inputField_yhmm.text = "";
        inputField_qrmm.text = "";
        inputField_yhdh.text = "";
        inputField_yhqq.text = "";

    }
    public void denglu()
    {
        if (inputField_zh.text == "" || inputField_mm.text == "")
        {
            ty_xxk("用户名或者密码不能为空!");
            return;
        }
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(_xmlpath);
        XmlNodeList nodeList = xmlDoc.SelectSingleNode("Root").ChildNodes;
        foreach(XmlElement xe in nodeList)
        {
            if(xe.GetAttribute("user_name") == inputField_zh.text)
            {
                var mima = xe.GetAttribute("user_pass");
                if(inputField_mm.text == mima)
                {
                    ty_xxk("登录成功!");
                    Application.OpenURL("http://wwww.4399.com");
                    return;
                }else
                {
                    ty_xxk("登录失败!密码错误!");
                    return;
                }
            }
        }
        ty_xxk("此用户账号还没有注册!请注册!");
    }

  

UGUI代码分析