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

推荐订阅源

SecWiki News
SecWiki News
N
Netflix TechBlog - Medium
D
Docker
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
U
Unit 42
Recorded Future
Recorded Future
G
Google Developers Blog
T
Threatpost
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Blog of Author Tim Ferriss
C
Cyber Attacks, Cyber Crime and Cyber Security
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
WordPress大学
WordPress大学
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
M
MIT News - Artificial intelligence
月光博客
月光博客
Spread Privacy
Spread Privacy
Know Your Adversary
Know Your Adversary
人人都是产品经理
人人都是产品经理
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Hacker News
The Hacker News
K
Kaspersky official blog
Simon Willison's Weblog
Simon Willison's Weblog
MongoDB | Blog
MongoDB | Blog
J
Java Code Geeks
I
Intezer
Y
Y Combinator Blog
P
Proofpoint News Feed
G
GRAHAM CLULEY
L
LINUX DO - 热门话题
Schneier on Security
Schneier on Security
B
Blog
Jina AI
Jina AI
V2EX - 技术
V2EX - 技术
雷峰网
雷峰网
Last Week in AI
Last Week in AI
V
V2EX
Martin Fowler
Martin Fowler
P
Palo Alto Networks Blog
Latest news
Latest news
Google DeepMind News
Google DeepMind News
L
Lohrmann on Cybersecurity
The Last Watchdog
The Last Watchdog

博客园 - 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;
    }
}