【簡単入門】3Dキャラクターのステータスを画像を使ってUI上にリアルタイムに表示する2つの方法

方法 1: コードの実装

効果:

1: UIを構築する

キャラクターをシーンに配置します。ここでは背景画像を作成しました。好みに応じて選択してください。

左下隅に画像を配置して、showRole という名前のキャラクターを表示します。

2: スクリプト <LockPlayer> をハングします。

コードは以下のように表示されます。

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

public class LockPlayer : MonoBehaviour
{
    public GameObject player;
    Camera camera;
    void Start()
    {
        //生成新相机,名字为cam
        GameObject cam = new GameObject("cam");
        camera = cam.AddComponent<Camera>();
        camera.clearFlags = CameraClearFlags.Color;
        //相机位置赋值
        camera.transform.position = player.transform.position + player.transform.forward + Vector3.up * 0.5f;
        //相机朝向
        camera.transform.LookAt(player.transform.position + Vector3.up * 0.5f);

        //纹理设置
        RenderTexture texture = new RenderTexture(256, 256,0);
        camera.targetTexture = texture;

        //材质设置
        Material material = new Material(Shader.Find("UI/Default"));
        material.mainTexture = texture;
        GetComponent<Image>().material = material;

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

3: キャラクターのドラッグ

おすすめ

転載: blog.csdn.net/m0_74022070/article/details/130106719