Version: 2017.2

Image.overrideSprite

切换到手册
public Sprite overrideSprite ;

描述

设置一个用于渲染的重写 Sprite。

overrideSprite 变量允许 Sprite 更改 Sprite。这种更改是立即发生的。当不再需要更改的 Sprite 时,可以将 Sprite 还原为 原始版本。当 overrideSprite 设置为 null 时,会发生这种情况。 \ 注意:下面的脚本示例有两个按钮。按钮纹理是从 Resources 文件夹加载的。(显示的示例中并未使用)。示例代码中添加了 两个 Sprite。Example1Example2 是按钮的 OnClick 函数调用的函数。Example1 调用 overrideSprite,Example2 将 overrideSprite 设置为 null。

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

public class ExampleClass : MonoBehaviour { private Sprite sprite1; private Sprite sprite2; private Image i;

public void Start() { i = GetComponent<Image>(); sprite1 = Resources.Load<Sprite>("texture1"); sprite2 = Resources.Load<Sprite>("texture2");

i.sprite = sprite1; }

// Called by a Button OnClick() with ExampleClass.Example1 // Uses overrideSprite to make this change temporary public void Example1() { i.overrideSprite = sprite2; }

// Called by a Button OnClick() with ExampleClass.Example2 // Removes the overrideSprite which causes the original sprite to be used again. public void Example2() { i.overrideSprite = null; } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961