unity 点击事件

目录

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

[第1步添加ui button,添加ui RawImage](#第1步添加ui button,添加ui RawImage)

[第2步 添加脚本:](#第2步 添加脚本:)

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

[GameObject添加 Component组件](#GameObject添加 Component组件)


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

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

第2步 添加脚本:

cs 复制代码
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 update

    private 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组件

相关推荐
垂葛酒肝汤3 小时前
Unity的UI扫光效果Shader
ui·unity·游戏引擎
mxwin6 小时前
Unity Shader Alpha测试 · 模板测试 · 深度测试
unity·游戏引擎
2601_956002819 小时前
冬日狂想曲(赠去马赛克补丁)2026.5.13最新版免费下载 转存后自动更新 (看到请立即转存 资源随时失效)pc手机版通用
智能手机·游戏引擎·电脑·游戏程序·动画·游戏美术
Sator110 小时前
unity解决粒子与物体接触时的硬边缘问题
unity·游戏引擎
RPGMZ12 小时前
RPGMZ NPC头顶自动显示一段消息
前端·游戏引擎·rpgmz·rpgmakermz
程序员JerrySUN14 小时前
Jetson边缘嵌入式实战课程第三讲:L4T 与 Jetson 系统架构
linux·服务器·人工智能·安全·unity·系统架构·游戏引擎
萌萌的提莫队长15 小时前
Unity HDRP 渲染管线 Camera 输出到RenderTexture没有Alpha通道
unity·游戏引擎
winlife_15 小时前
Unity Editor 工具不该用反射写组件字段:SerializedObject 在 4 个场景里非用不可
unity·自动化·游戏引擎
星河耀银海16 小时前
Unity C#入门:变量的定义与访问权限(public/private)
unity·c#·lucene
郝学胜-神的一滴16 小时前
中级OpenGL教程 005:为球体&平面注入法线灵魂
c++·unity·图形渲染·three.js·opengl·unreal