unity image动态调整Rect大小

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class maodian : MonoBehaviour {
public Image o;
float oo=3.35f;
float ooo = 350.2f;
float tim;

bool panduan = true;
// Use this for initialization
void Start () {
    tim = Time.time + 0.1f;
}

// Update is called once per frame
void Update () {
    if (Time.time>tim&&panduan)
    {
        print(tim);
        oo -= 3;
        ooo -= 6;
        tim += 0.1f;
        if (oo<=-166)
        {
            Destroy(o);
            panduan = false;
        }

    }

    o.GetComponent<RectTransform>().anchoredPosition = new Vector2(106.8f, oo);//改变pos.y的大小
  o.  GetComponent<RectTransform>().sizeDelta = new Vector2(597.8f, ooo);//改变height的大小
}

}

Unity 中,可以通过脚本动态加载并设置系统图片到 `Image` 组件上。这通常涉及将本地路径中的图片文件读取为字节流,并将其转换为纹理 (Texture),最后应用到 UI 的 Image 组件中。 以下是实现这一功能的具体方法: ### 动态设置系统图片到 Image 组件 #### 加载外部图片 可以使用 `WWW` 或者更现代的 `UnityWebRequest` 来加载来自本地或者网络上的图片资源。对于本地图片,需提供其绝对路径[^1]。 ```csharp using System.Collections; using UnityEngine; using UnityEngine.UI; public class LoadSystemImage : MonoBehaviour { public RawImage rawImage; // 如果需要支持高分辨率图像,建议使用RawImage代替Image public string imagePath = "file://C:/path/to/your/image.png"; // 替换为你自己的图片路径 void Start() { StartCoroutine(LoadImage()); } IEnumerator LoadImage() { using (var www = new WWW(imagePath)) // 使用WWW类来加载指定路径下的图片 { yield return www; // 等待直到图片完全下载完成 Texture2D texture = www.texture; // 获取加载后的纹理数据 if (rawImage != null && texture != null) { rawImage.texture = texture; // 将获取到的纹理赋值给RawImage组件 } } } } ``` 注意:如果目标是普通的 `Image` 而不是 `RawImage`,则还需要额外创建一个 `Sprite` 对象再分配给它。 ```csharp if (image != null && texture != null){ Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f); image.sprite = sprite; } ``` 上述代码片段展示了如何通过 C# 编写脚本来从特定位置加载一张图片,并把它显示在一个 `RawImage` 或常规 `Image` 上面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值