惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
V
V2EX
B
Blog
腾讯CDC
D
DataBreaches.Net
H
Hackread – Cybersecurity News, Data Breaches, AI and More
月光博客
月光博客
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
Latest news
Latest news
L
LINUX DO - 最新话题
C
Check Point Blog
Attack and Defense Labs
Attack and Defense Labs
H
Hacker News: Front Page
Forbes - Security
Forbes - Security
H
Help Net Security
S
Securelist
D
Docker
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Last Watchdog
The Last Watchdog
N
News and Events Feed by Topic
G
Google Developers Blog
AI
AI
I
Intezer
L
LangChain Blog
NISL@THU
NISL@THU
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
罗磊的独立博客
T
Tenable Blog
Jina AI
Jina AI
I
InfoQ
C
Cybersecurity and Infrastructure Security Agency CISA
Cisco Talos Blog
Cisco Talos Blog
C
CERT Recently Published Vulnerability Notes
GbyAI
GbyAI
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
O
OpenAI News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Know Your Adversary
Know Your Adversary
T
Troy Hunt's Blog
Google Online Security Blog
Google Online Security Blog

博客园 - kingBook

git patch git 修改最后一次提交的日期 Win10 修改特定格式文件的右键快捷菜单 TypeScript async、 await、Promise LayaAir3.x 侦听程序退出 旋转力学例子 旋转力学公式 凹多边形碰撞检测 LayaAir3.x 侦听键盘事件 URP 阴影 TypeScript 里的 override TypeScript 类的自身类型 Unity 二维数组序列化 C# 匿名对象、动态属性 Cocos Creator 安卓模拟器中无法运行 LayaAir3.2.0-beta.2 设置2d刚体线性速度,在不同设备(分辨率)下,表现不一致的问题 LayaAir3.x 物理2D碰撞事件 TypeScirpt 声明Map类型变量 TypeScript 声明函数类型变量
Unity Editor 保存图片、缩放纹理
kingBook · 2024-08-16 · via 博客园 - kingBook
using System.IO;
using UnityEditor;
using UnityEngine;

public class ConvertIconToMultipleSizes : Editor {
    [MenuItem("Assets/Convert Icon To Multiple Sizes", true)]
    private static bool ValidateSplitFbxAnimation() {
        if (Selection.count == 1) {
            if (Selection.activeObject is Texture2D) {
                return true;
            }
        }
        return false;
    }

    [MenuItem("Assets/Convert Icon To Multiple Sizes")]
    private static void SplitFbxAnimation() {
        if (Selection.count == 1) {
            if (Selection.activeObject is Texture2D) {
                Texture2D source = Selection.activeObject as Texture2D;
                string sourcePath = AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]);

                Texture2D result = ScaleTexture(source, 36, 36);
                byte[] bytes = result.EncodeToPNG();

                string sourceFolderPath = Path.GetDirectoryName(sourcePath).Replace("\\", "/");
                string newFolderPath = sourceFolderPath + "/Icons";

                if (!File.Exists(newFolderPath)) {
                    Directory.CreateDirectory(newFolderPath);
                }

                string outputPath = $"{newFolderPath}/icon36x36.png";
                File.WriteAllBytes(outputPath, bytes);
                AssetDatabase.Refresh();
            }
        }
    }


    /// <summary>
    /// 缩放纹理
    /// <para>
    /// 注意:
    /// source 源纹理的 Read/Write 开关需要在 Inspector 中打开,
    /// 不能勾选 Use Crunch Compression
    /// </para>
    /// </summary>
    public static Texture2D ScaleTexture(Texture2D source, int targetWidth, int targetHeight) {
        Texture2D result = new(targetWidth, targetHeight);
        Color[] rpixels = new Color[targetWidth * targetHeight];
        float incX = ((float)1 / source.width) * ((float)source.width / targetWidth);
        float incY = ((float)1 / source.height) * ((float)source.height / targetHeight);
        for (int px = 0; px < rpixels.Length; px++) {
            rpixels[px] = source.GetPixelBilinear(incX * ((float)px % targetWidth), incY * (Mathf.Floor((float)px / targetWidth)));
        }
        result.SetPixels(rpixels, 0);
        result.Apply();
        return result;
    }
}