創作內容

2 GP

Unity 程式功能 (4) - 繪製扇形(Sprite Ver)

作者:三角形│2020-11-17 18:24:42│巴幣:4│人氣:113
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DrawSector : MonoBehaviour {
    // 空白區域色值
    [SerializeField] Color emptyColor = new Color(0, 0, 0, 0);
    // 圓環區域色值
    [SerializeField] Color circleColor = new Color(1, 0, 0, 0.5f);

    // 圓環內徑/外徑
    [SerializeField] int minRadius = 80;
    [SerializeField] int maxRadius = 100;
    // 扇形角度
    [SerializeField] float circleAngle = 90;

    SpriteRenderer spriteRenderer;

    private void Awake() {
        spriteRenderer = GetComponent<SpriteRenderer>();
        if (spriteRenderer == null) {
            gameObject.AddComponent<SpriteRenderer>();
        }
    }

    private void Update() {
        Draw();
    }

    public void Draw() {
        spriteRenderer.sprite = CreateSprite(minRadius, maxRadius, circleAngle / 2, circleColor);
    }

    /// <summary>
    /// 繪製扇形圓環,生成 Sprite
    /// </summary>
    /// <param name="minRadius">圓環內徑,值為0即是實心圓</param>
    /// <param name="maxRadius">圓環外徑</param>
    /// <param name="halfAngle">1/2扇形弧度,值>=180度即是整圓</param>
    /// <param name="circleColor"></param>
    /// <returns></returns>
    Sprite CreateSprite(int minRadius, int maxRadius, float halfAngle, Color circleColor) {
        // 圖片尺寸
        int spriteSize = maxRadius * 2;
        // 創建 Texture2D
        Texture2D texture2D = new Texture2D(spriteSize, spriteSize);
        // 圖片中心像素點座標
        Vector2 centerPixel = new Vector2(spriteSize / 2, spriteSize / 2);
        //
        Vector2 tempPixel;
        float tempAngle, tempDisSqr;
        if (halfAngle > 0 && halfAngle < 360) {
            // 遍歷像素點,繪製扇形圓環
            for (int x = 0; x < spriteSize; x++) {
                for (int y = 0; y < spriteSize; y++) {
                    // 以中心為起點,獲取像素點向量
                    tempPixel.x = x - centerPixel.x;
                    tempPixel.y = y - centerPixel.y;
                    // 是否在半徑範圍內
                    tempDisSqr = tempPixel.sqrMagnitude;
                    if (tempDisSqr >= minRadius * minRadius && tempDisSqr <= maxRadius * maxRadius) {
                        // 是否在角度範圍內
                        tempAngle = Vector2.Angle(Vector2.up, tempPixel);
                        if (tempAngle < halfAngle || tempAngle > 360 - halfAngle) {
                            texture2D.SetPixel(x, y, circleColor);
                            continue;
                        }
                    }
                    // 設置為透明
                    texture2D.SetPixel(x, y, emptyColor);
                }
            }
        }
        else {
            // 遍歷像素點,繪製圓環
            for (int x = 0; x < spriteSize; x++) {
                for (int y = 0; y < spriteSize; y++) {
                    // 以中心為起點,獲取像素點向量
                    tempPixel.x = x - centerPixel.x;
                    tempPixel.y = y - centerPixel.y;
                    // 是否在半徑範圍內
                    tempDisSqr = tempPixel.sqrMagnitude;
                    if (tempDisSqr >= minRadius * minRadius && tempDisSqr <= maxRadius * maxRadius) {
                        // 設置像素色值
                        texture2D.SetPixel(x, y, circleColor);
                        continue;
                    }
                    // 設置為透明
                    texture2D.SetPixel(x, y, emptyColor);
                }
            }
        }
        texture2D.Apply();

        Sprite sprite;
        sprite = Sprite.Create(texture2D, new Rect(0, 0, spriteSize, spriteSize), new Vector2(0.5f, 0.5f));

        return sprite;
    }


}

參考資料: https://www.twblogs.net/a/5e94fd90bd9eee34209033a2
引用網址:https://home.gamer.com.tw/TrackBack.php?sn=4984472
All rights reserved. 版權所有,保留一切權利

相關創作

同標籤作品搜尋:Unity

留言共 0 篇留言

我要留言提醒:您尚未登入,請先登入再留言

2喜歡★denny95090 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:Unity 程式功能 (...

追蹤私訊切換新版閱覽

作品資料夾

d88931122所有巴友
老僧的Steam遊戲新作《蘿莉RACING》已上市,歡迎參考 : https://home.gamer.com.tw/artwork.php?sn=5890881看更多我要大聲說11小時前


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】