zoukankan      html  css  js  c++  java
  • unity3d-绘制贴图

    准备贴图

    在屏幕在绘制一张静态贴图,需要用到GUI.DrawTexture()方法, 该方法可以设定图片的显示位置、缩放比例和渲染混合等

           /*
                   Rect position:表示图片的绘制区域
                 * Texture image:表示绘制图片的对象
                 * ScaleMode scaleMode:表示图片的缩放模式
                 * bool alphaBlend:表示十分开启图片混合模式
                 * float imageAspect:表示图片的缩放宽高比例
                 */
                 public static void DrawTexture(Rect position, Texture image, ScaleMode scaleMode, bool alphaBlend, float imageAspect);
    在Project视图中将需要加载的图片保存在根目录 "Resources" 中,记住 一定要保存在Resources中,这是unity3d规定的,否则无法加载!!
     

    加载并绘制贴图

    加载贴图使用Resources.Load()和Resources.LoadAll()方法,
     
     1 using UnityEngine;
     2 using System.Collections;
     3 
     4 public class loadText2d : MonoBehaviour
     5 {
     6 
     7     private Texture2D txt;
     8     Texture2D[] array;
     9     // Use this for initialization
    10     void Start()
    11     {
    12 
    13     }
    14 
    15     // Update is called once per frame
    16     void Update()
    17     {
    18 
    19     }
    20     void OnGUI()
    21     {
    22         //加载贴图
    23         if (GUI.Button(new Rect(10, 50, 90, 90), "加载一张图片"))
    24         {
    25             txt = Resources.Load<Texture2D>("one/0");
    26         }
    27         if (GUI.Button(new Rect(10, 150, 90, 90), "加载所有图片"))
    28         {
    29             array = Resources.LoadAll<Texture2D>("more");
    30         }
    31         //绘制贴图
    32         if (txt != null)
    33         {
    34             GUI.DrawTexture(new Rect(150, 50, 90, 90), txt, ScaleMode.StretchToFill, true, 0);
    35         }
    36         if (array != null)
    37         {
    38             for (int i = 0; i < array.Length; i++)
    39             {
    40                 GUI.DrawTexture(new Rect(150 + i * 95, 150, 90, 90), array[i], ScaleMode.ScaleToFit, true, 0);
    41             }
    42             
    43 
    44         }
    45     }
    46 }

    Demo演示

  • 相关阅读:
    卡牌配对
    SNOI2017 礼物
    【BZOJ2893】征服王
    景中人
    钦点
    杨柳
    兼容IE与firefox、chrome的css 线性渐变(linear-gradient)
    使用C# DES解密java DES加密的字符串
    jQuery插件autoComplete使用
    hadoop SQL使用
  • 原文地址:https://www.cnblogs.com/nsky/p/4451325.html
Copyright © 2011-2022 走看看