UGUI Image图像控件替换图片

代码为探索而来,不是最优代码,请按需使用。

Unity3d引擎版本:Uinty3d 20233.2.3f1

补充一下图片如何改成Texture2D:

1、将图片导入unity。

2、选择图片,按下图操作,点击应用即可。

脚本代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ImgVolumeScrpt : MonoBehaviour,IPointerClickHandler
{
    //Texture2D texture_CPR_btn_Sound_Open = Resources.Load<Texture2D>("Images/CPR_btn_Sound_Open");

    public Texture2D texture_btn_Sound_Open;
    public Texture2D texture_btn_Sound_Off;
    private bool isOpen = true;

    public void OnPointerClick(PointerEventData eventData)
    {
        UnityEngine.UI.Image img =this.GetComponent("Image") as UnityEngine.UI.Image;
        if(!isOpen)
        {
            Sprite sprite = Sprite.Create(texture_btn_Sound_Open, new Rect(0, 0, texture_btn_Sound_Open.width, texture_btn_Sound_Open.height), new Vector2(1.0f, 1.0f));
            img.sprite = sprite;
        }
        else
        {
            Sprite sprite = Sprite.Create(texture_btn_Sound_Off, new Rect(0, 0, texture_btn_Sound_Off.width, texture_btn_Sound_Off.height), new Vector2(1.0f, 1.0f));
            img.sprite = sprite;
        }
        isOpen = !isOpen;
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

相关推荐

  1. 18. 【Android教程】图片 ImageView

    2024-01-10 16:06:02       14 阅读
  2. ASP.NET之图像

    2024-01-10 16:06:02       10 阅读
  3. WPF 使用Image显示图片

    2024-01-10 16:06:02       6 阅读
  4. MFC Picture显示图片并适应大小

    2024-01-10 16:06:02       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-10 16:06:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-10 16:06:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-10 16:06:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-10 16:06:02       20 阅读

热门阅读

  1. Istio 专栏目录

    2024-01-10 16:06:02       35 阅读
  2. Python的进制转换

    2024-01-10 16:06:02       35 阅读
  3. SkyWalking相关问题及答案(2024)

    2024-01-10 16:06:02       45 阅读
  4. PCL 点云八叉树射线相交

    2024-01-10 16:06:02       39 阅读
  5. 阶乘数码#洛谷

    2024-01-10 16:06:02       63 阅读
  6. 运维工程师的困境和解困之道

    2024-01-10 16:06:02       39 阅读
  7. 八股文 c++ 多态

    2024-01-10 16:06:02       28 阅读
  8. kotlin的抽象类和抽象方法

    2024-01-10 16:06:02       33 阅读
  9. WinForm 中Label自动换行 解决方法 Label自动换行

    2024-01-10 16:06:02       36 阅读
  10. 【数组】力扣704二分查找

    2024-01-10 16:06:02       40 阅读
  11. QT 简单连接WIFI模块

    2024-01-10 16:06:02       38 阅读
  12. 帕金森病的原因是什么?

    2024-01-10 16:06:02       40 阅读
  13. Linux进阶命令使用

    2024-01-10 16:06:02       38 阅读