@H_404_4@
不勾选:拉伸至整个显示框
Sliced(切片):优化图片的尺寸
Fill Center:是否填充中心部分
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UGUI_Image : MonoBehavIoUr { //public UnityEngine.UI.Image image;//另一种引用形式 private Image image; private Sprite sp; private Sprite sp1; private float count = 0; private void Awake() { image = GetComponent<Image>(); //这种形式加载不到Sprite类型的图片 //错误加载方法:sp = Resources.Load("Texture/名字") as Sprite; //需要使用泛型的方法去加载Sprite的图片 sp = Resources.Load<Sprite>("Texture/名字"); //Sprite多图模式的加载,加载Spirite方式:数据结构(字典)来做管理(略) //加载多图形式下的所有的sprite,返回的是一个数组。 Sprite[] sp_arr = Resources.LoadAll<Sprite>("Texture/data.dat 00003");//按名字索引图片 string strName = "data.dat 00003_17"; foreach (var item in sp_arr) { //Debug.Log(item.name); if (item.name == strName) { sp = item; } } //sp = sp_arr[18]; } // Use this for initialization void Start () { //在原有图片上覆盖一张图片 //image.overrideSprite = sp; //改变源图片 //image.sprite = sp; //image.overrideSprite = sp1; image.color = Color.red; //在填充模式下改变填充值 //image.fillAmount = 0.5f;//fillMethod,枚举类型;fillOrigin,int类型,也代表枚举 } // Update is called once per frame void Update () { if (Input.GetKeyDown(KeyCode.P)) { image.sprite = sp; //image.overrideSprite = null; } //count += Time.deltaTime; //image.fillAmount = count; } }多图模式下,也可以使用切片
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ClickChangeImage : MonoBehavIoUr { public Image image; public Text text; public string str;//替换的文字内容 public string spriteName; private Sprite sp; private Button button; // Use this for initialization void Awake () { button = GetComponent<Button>(); button.onClick.AddListener(ClickButton); } void ClickButton() { text.text = str; if (sp == null) { sp = Resources.Load<Sprite>("Tex/" + spriteName); } image.sprite = sp; } }
代码操作 Texture类型的图片可以这么加载
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UGUI_RawImage : MonoBehavIoUr { public RawImage image; private Texture tex; // Use this for initialization void Start () { //对于Texture类型的图片使用以下两种方式都是可以加载成功的 //tex = Resources.Load("Texture/XM2") as Texture; tex = Resources.Load<Texture>("Texture/XM2"); //改变RawImage的图片 image.texture = tex; //设置图片在显示框的偏移和大小比例 //image.uvRect = new Rect(x,y,w,h); } // Update is called once per frame void Update () { } }
代码操作
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UGUI_Text : MonoBehavIoUr { public Text text; // Use this for initialization void Start () { text.text = "小明老师又吃<b><color=red>腰子</color></b>!"; //代码里的RGB的取值范围是0-1, 色板上的是 0 - 255 text.color = new Color(230 / 255f, 0, 255 / 255f);//230 0 255 text.fontSize = 30; } // Update is called once per frame void Update () { } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UGUI_Button : MonoBehavIoUr { public Button button; //1.先声明一个委托类型, 无返回值,无参数的 public delegate void Del(); //2.声明一个委托变量 public Del del; // Use this for initialization void Start () { //3.添加方法进委托, 只有无返回值无参数的方法才能添加到del的委托变量里 del += TestDel; //4.委托的调用跟方法一样的,调用之前一定要做判null处理 if (null != del) { del(); } //把一个方法作为参数传递到了另一个方法的内部 Test(TestDel); //使用代码添加按钮执行事件 button.onClick.AddListener(AddClickButton); //删除事件 //button.onClick.RemoveListener(AddClickButton); //使用Invoke方法去执行了一次OnClick里存储的所有的方法 button.onClick.Invoke(); } // Update is called once per frame void Update () { } //委托类型作为参数 void Test(Del de) { if (de != null) { de(); } } void TestDel() { Debug.Log("TestDel"); } public void ClickButton(GameObject str1) { Debug.Log("你点击了按钮!" + str1); } public void ClickButton(string str) { Debug.Log("你点击了按钮!" + str); } public void ClickButton() { Debug.Log("你点击了按钮!"); } void AddClickButton() { Debug.Log("脚本添加的方法"); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。