当前位置: 首页 > news >正文

unity 点击事件

目录

点击按钮,显示图片功能教程

第1步添加ui button,添加ui RawImage

第2步 添加脚本:

第3步,把脚本拖拽到button,点击button,设置脚本的变量,

GameObject添加 Component组件


点击按钮,显示图片功能教程

第1步添加ui button,添加ui RawImage

第2步 添加脚本:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;public class LoadImageByString : MonoBehaviour
{public RawImage showImage;public Button loadImage;private string imgPath;private string imageStr;// Start is called before the first frame updateprivate void Awake(){Debug.Log("醒了");imgPath = @"C:\Users\Administrator\Pictures\mm\pics\005953_4.jpg";Debug.Log(imgPath);imageStr = SetImageToString(imgPath);}void Start(){Debug.Log("醒了");imgPath = @"C:\Users\Administrator\Pictures\mm\pics\005953_4.jpg";Debug.Log(imgPath);imageStr = SetImageToString(imgPath);//  loadImage = gameObject.GetComponent<Button>();//  showImage = gameObject.GetComponent<RawImage>();Debug.Log("开始");// showImage.texture = GetTextureByString(imageStr);loadImage.onClick.AddListener(() =>{showImage.texture = GetTextureByString(imageStr);});}/// <summary>/// 将图片转化为字符串/// </summary>private string SetImageToString(string imgPath){Debug.Log("将图片转化为字符串");FileStream fs = new FileStream(imgPath, FileMode.Open);byte[] imgByte = new byte[fs.Length];fs.Read(imgByte, 0, imgByte.Length);fs.Close();return Convert.ToBase64String(imgByte);}/// <summary>/// 将字符串转换为纹理/// </summary>private Texture2D GetTextureByString(string textureStr){Debug.Log("将字符串转换为纹理");Texture2D tex = new Texture2D(10, 10);byte[] arr = Convert.FromBase64String(textureStr);tex.LoadImage(arr);tex.Apply();return tex;}public void onclick(){Debug.Log("按下了");}
}

第3步,把脚本拖拽到button,点击button,设置脚本的变量,

运行,点击button,看RawImage显示图片,至此ok。 

左边选择场景,canvas,选择button,正常情况下,onclick会关联事件函数。

但是从网上开源项目改的,就没有。

GameObject添加 Component组件

http://www.lryc.cn/news/298708.html

相关文章:

  • idea自带的HttpClient使用
  • vue3-应用规模化-路由和状态
  • 网络安全检查表
  • SSM框架,Maven的学习(下)
  • Vivado开发FPGA使用流程、教程 verilog(建立工程、编译文件到最终烧录的全流程)
  • C语言之动态内存管理
  • 【AIGC风格prompt深度指南】掌握绘画风格关键词,实现艺术模仿的革新实践
  • Qt安装配置教程windows版(包括:Qt5.8.0版本,Qt5.12,Qt5.14版本下载安装教程)(亲测可行)
  • SpringCloud-Ribbon实现负载均衡
  • Qt网络编程-TCP与UDP
  • Promise 常见题目
  • 五大架构风格之五:仓库架构风格
  • 探索设计模式的魅力:外观模式简化术-隐藏复杂性,提供简洁接口的设计秘密
  • java之Maven
  • Elasticsearch(四)
  • 蓝桥杯-X图形
  • 2. Maven 继承与聚合
  • 如何把手机平板变为电脑的屏幕
  • Amazon Dynamo学习总结
  • appium抓包总结
  • arcgis各种版本下载
  • 第五篇:MySQL常见数据类型
  • Oracle用BETWEEN AND查某年的数据可能会丢失条数
  • Nuscenes数据集点云数据如何转换到图像上
  • 【C语言期末】商品管理系统
  • 单片机学习笔记---串口通信(2)
  • 【Java】乐观锁有哪些常见实现方式?
  • Javaweb之SpringBootWeb案例之登录校验功能的详细解析
  • CSS之盒模型
  • 博客系统-SpringBoot版本