1. 程式人生 > >Unity3D 人形血條製作小知識

Unity3D 人形血條製作小知識

這幾天用Unity3D做個射擊小遊戲,想做個人形的血條,百思不得其解,後來問了網上的牛牛們,解決了,其實挺簡單的,GUI裡面有個函式DrawTextureWithTexCoords就可以實現圖片的裁剪,很方便,裁剪之後,相信大家就都會操作了,話不多說,貼上程式碼與截圖效果。

using UnityEngine;

using System.Collections;

using   UnityEditor;

public class drawTexture : MonoBehaviour {

    public Texture back;//背景圖

    public Texture fore;//前景圖

    private float scale_height = 0.99f;

    private float position_left=500f;

    private float position_top=300f;

    // Use this for initialization

    void Start () {

    }

    void OnGUI()

    {

        GUI.DrawTexture (new Rect (position_left, position_top, back.width, back.height), back);

        //GUI.DrawTexture (new Rect (500, 300, fore.width, fore.height), fore);

        if (Input.GetKeyUp(KeyCode.Space)) {

            scale_height=scale_height- 0.01f;

            if(scale_height<0)

                scale_height=0;

                }

        GUI.Label (new Rect(0,0,100,100),"scale_height"+scale_height);

        GUI.DrawTextureWithTexCoords (new Rect (position_left, position_top+back.height*(1-scale_height), fore.width, fore.height*scale_height), fore,

                                   new Rect(0,0,1, scale_height),true);

    }

    // Update is called once per frame

    void Update () {

    }

}


效果圖:




效果還不錯吧,哈哈。