学做网站基础知识,WordPress怎么添加留言功能,网站后台看不到部分内容,网站 多个ip 备案目录
点击按钮#xff0c;显示图片功能教程
第1步添加ui button#xff0c;添加ui RawImage
第2步 添加脚本#xff1a;
第3步#xff0c;把脚本拖拽到button#xff0c;点击button#xff0c;设置脚本的变量#xff0c;
GameObject添加 Component组件 点击按钮显示图片功能教程
第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.GetComponentButton();// showImage gameObject.GetComponentRawImage();Debug.Log(开始);// showImage.texture GetTextureByString(imageStr);loadImage.onClick.AddListener(() {showImage.texture GetTextureByString(imageStr);});}/// summary/// 将图片转化为字符串/// /summaryprivate 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/// 将字符串转换为纹理/// /summaryprivate 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组件