第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 Unity 繪制應(yīng)用程序

使用 Unity 繪制應(yīng)用程序

C#
慕妹3146593 2022-12-31 14:00:23
在過去的一周里,我一直在尋找一種使用 Unity 創(chuàng)建應(yīng)用程序的方法,我將能夠在其中創(chuàng)建繪圖。是這樣的:我嘗試了幾種模擬繪圖的方法,但都沒有達(dá)到我的預(yù)期。您認(rèn)為實(shí)現(xiàn)這一目標(biāo)的推薦方法是什么?我嘗試使用 SetPixel 和 GetPixel,但由于在運(yùn)行時(shí)編輯紋理可能非常慢,我正在尋找更好的方法(如果有的話)。謝謝。
查看完整描述

1 回答

?
汪汪一只貓

TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊

我沒有用相對較大的紋理對此進(jìn)行測試,但我用它畫了一張快樂的臉,我有評論解釋代碼中發(fā)生了什么,使用它的最佳方法是使用 Unity 菜單并創(chuàng)建一個(gè) RawImage,然后附加此腳本:


using UnityEngine;

using UnityEngine.UI;


[RequireComponent(typeof(RawImage))]

public class PaintCanvas : MonoBehaviour

{


    RectTransform rt;

    RawImage ri;

    Vector3 bottomLeft = Vector3.zero;

    Vector3 topRight = Vector3.zero;

    Texture2D canvas;


    int width = 0;

    int height = 0;


    // Start is called before the first frame update

    void Start()

    {

        // Getting the RectTransform, since this is a RawImage, which exists on the canvas and should have a rect transform

        rt = GetComponent<RectTransform>();

        if (rt != null)

        {

            GetWorldCorners();

        }

        // RawImage that we are going to be updating for our paint application.

        ri = GetComponent<RawImage>();

        if (ri != null)

        {

            CreateTexture2D();

        }

    }


    // Update is called once per frame

    void Update()

    {

        // Make sure our stuff is valid

        if (rt != null)

        {

            if(ri != null)

            {

                HandleInput();

            }

        }

    }


    void HandleInput()

    {

        // Since we can only paint on the canvas if the mouse button is press

        // May be best to revise this so the tool has a call back for example a 

        // fill tool selected would call its own "Handle" method,


        if(Input.GetMouseButtonDown(0) || Input.GetMouseButton(0))

        {

            Vector2Int mousePos = Vector2Int.zero;

            // We have input, lets convert the mouse position to be relative to the canvas

            ConvertMousePosition(ref mousePos);

            // Checking that our mouse is in bounds, which is stored in our height and width variable and as long as it has a "positive value"

            if(MouseIsInBounds(mousePos))

            {

                // This method could be removed to be the tool method I mention above

                // you would pass in the mousePosition, and color similar to this.

                // This way each tool would be its "own" component that would be activated

                // through some form of UI.

                PaintTexture(mousePos, Color.black); // Also the color you want would be here to...

            }


            Debug.Log(mousePos);

        }

    }


    void PaintTexture(Vector2Int pos, Color color)

    {

        // In our method we don't allow transparency and we are just replacing the pixel,

        canvas.SetPixel(pos.x, pos.y, color);

        // Applying out change, we dont want to mip levels.

        // If you are doing some blending or transparency stuff that would be handled by your tool

        canvas.Apply(false);

    }


    bool MouseIsInBounds(Vector2Int mousePos)

    {

        // The position is already relative to the texture so if it is >= to 0 and less then the texture

        // width and height it is in bounds.

        if(mousePos.x >= 0 && mousePos.x < width)

        {

            if (mousePos.y >= 0 && mousePos.y < height)

            {

                return true;

            }

        }

        return false;

    }


    void ConvertMousePosition(ref Vector2Int mouseOut)

    {

        // The mouse Position, and the RawImage position are returned in the same space

        // So we can just update based off of that

        mouseOut.x = Mathf.RoundToInt(Input.mousePosition.x - bottomLeft.x);

        mouseOut.y = Mathf.RoundToInt(Input.mousePosition.y - bottomLeft.y);

    }


    void CreateTexture2D()

    {

        // Creating our "Draw" texture to be the same size as our RawImage.

        width = Mathf.RoundToInt(topRight.x - bottomLeft.x);

        height = Mathf.RoundToInt(topRight.y - bottomLeft.y);

        canvas = new Texture2D(width, height);

        ri.texture = canvas;

    }


    void GetWorldCorners()

    {

        if (rt != null)

        {

            Vector3[] corners = new Vector3[4];

            rt.GetWorldCorners(corners);


            // Setting our corners  based on the fact GetCorners returns them in clockwise order starting from BL TL TR BR.

            bottomLeft = corners[0];

            topRight = corners[2];

        }

    }

}

我只想指出,取決于你的規(guī)模,你移動(dòng)鼠標(biāo)的速度有多快,你最終可能會(huì)得到點(diǎn),這是因?yàn)檫@個(gè)算法只更新每幀鼠標(biāo)經(jīng)過的像素,如果你移動(dòng)鼠標(biāo)快于 1 px每幀你都會(huì)有間隙,這可以通過存儲(chǔ)最后一幀的 mousePosition 來解決,獲取這一幀的新位置,并創(chuàng)建一條線,然后更新該線上的所有點(diǎn)。


查看完整回答
反對 回復(fù) 2022-12-31
  • 1 回答
  • 0 關(guān)注
  • 195 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)