Unity编辑器拓展——自定义包新建文件夹,自定义manifest和下载包

自定义包——新建文件夹

情景--------------------------

看油管的。写链接敏感了。
Editor + 程序集定义(资源,创建。不加不会运行) + json。以下三个文件/夹按下面步骤得到
在这里插入图片描述

Editor

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
using static System.IO.Directory;
using static System.IO.Path;
using static UnityEngine.Application;
using static UnityEditor.AssetDatabase;
using UnityEditor;

namespace NewBehaviourScript00
{
    public static class ToolsMenu
    {
        
        [MenuItem("My/Setup/Create Default Folders")]
        public static void CreateDefaultFolders()
        {
            Dir( "_Project","Scripts","Art","Scene");
            Refresh();
        }

        public static void Dir(string root, params string[] dir)
        {
            var fullPath = Combine( dataPath, root);
            foreach (var newDirectory in dir)
            {
                CreateDirectory( Combine(fullPath, newDirectory));
            }
        }
    }
}

程序集定义

在这里插入图片描述

json

{
  "name": "com.wuweisheng.tools",
  "version": "0.0.1-preview.1",
  "displayName": "创建目录"
}

效果---------------------

在这里插入图片描述

自定义manifest和下载包

精简manifest前---------------------------------------------

按视频前后差异
类似目录D:\Data\Projects\Unity\U_Test\Packages\manifest.json(不允许//注释符,下面只是看Up删除了哪些)

{
  "dependencies": {
    "com.unity.collab-proxy": "1.13.5",
    "com.unity.ide.rider": "3.0.7",
    //"com.unity.ide.visualstudio": "2.0.11",//负责比如unity双击脚本后打开visual studio
    //"com.unity.ide.vscode": "1.2.4",
    //"com.unity.test-framework": "1.1.29",
    "com.unity.textmeshpro": "3.0.6",
    "com.unity.timeline": "1.6.2",
    "com.unity.ugui": "1.0.0",
    //"com.unity.visualscripting": "1.7.2",
    "com.unity.modules.ai": "1.0.0",
    "com.unity.modules.androidjni": "1.0.0",
    "com.unity.modules.animation": "1.0.0",
    //"com.unity.modules.assetbundle": "1.0.0",
    "com.unity.modules.audio": "1.0.0",
    //"com.unity.modules.cloth": "1.0.0",
    //"com.unity.modules.director": "1.0.0",
    "com.unity.modules.imageconversion": "1.0.0",
    "com.unity.modules.imgui": "1.0.0",
    "com.unity.modules.jsonserialize": "1.0.0",
    "com.unity.modules.particlesystem": "1.0.0",
    "com.unity.modules.physics": "1.0.0",
    //"com.unity.modules.physics2d": "1.0.0",
    //"com.unity.modules.screencapture": "1.0.0",
    //"com.unity.modules.terrain": "1.0.0",
    //"com.unity.modules.terrainphysics": "1.0.0",
    //"com.unity.modules.tilemap": "1.0.0",
    "com.unity.modules.ui": "1.0.0",
    "com.unity.modules.uielements": "1.0.0",
    "com.unity.modules.umbra": "1.0.0",
    //"com.unity.modules.unityanalytics": "1.0.0",
    "com.unity.modules.unitywebrequest": "1.0.0",
    "com.unity.modules.unitywebrequestassetbundle": "1.0.0",
    //"com.unity.modules.unitywebrequestaudio": "1.0.0",
    //"com.unity.modules.unitywebrequesttexture": "1.0.0",
    "com.unity.modules.unitywebrequestwww": "1.0.0"
    //"com.unity.modules.vehicles": "1.0.0",
    //"com.unity.modules.video": "1.0.0",
    //"com.unity.modules.vr": "1.0.0",
    //"com.unity.modules.wind": "1.0.0",
    //"com.unity.modules.xr": "1.0.0"
  }
}

精简manifest后

{
  "dependencies": {
    "com.unity.collab-proxy": "1.13.5",
    "com.unity.ide.rider": "3.0.7",
    "com.unity.textmeshpro": "3.0.6",
    "com.unity.timeline": "1.6.2",
    "com.unity.ugui": "1.0.0",
    "com.unity.modules.ai": "1.0.0",
    "com.unity.modules.androidjni": "1.0.0",
    "com.unity.modules.animation": "1.0.0",
    "com.unity.modules.audio": "1.0.0",
    "com.unity.modules.imageconversion": "1.0.0",
    "com.unity.modules.imgui": "1.0.0",
    "com.unity.modules.jsonserialize": "1.0.0",
    "com.unity.modules.particlesystem": "1.0.0",
    "com.unity.modules.physics": "1.0.0",
    "com.unity.modules.ui": "1.0.0",
    "com.unity.modules.uielements": "1.0.0",
    "com.unity.modules.umbra": "1.0.0",
    "com.unity.modules.unitywebrequest": "1.0.0",
    "com.unity.modules.unitywebrequestassetbundle": "1.0.0",
    "com.unity.modules.unitywebrequestwww": "1.0.0"
  }
}

用精简的manifest拿到gist ID

在这里插入图片描述

下载包的代码(放Editor)------------------------

using UnityEngine;
using Random = UnityEngine.Random;
using static System.IO.Directory;
using static System.IO.Path;
using static UnityEngine.Application;
using static UnityEditor.AssetDatabase;
using UnityEditor;
using System.Threading.Tasks;
using System.Net.Http;
using System.IO;
using System;

namespace NewBehaviourScript00
{
    
    public static class ToolsMenu
    {
        public const string topItem = "MyTools";
     
        #region 导包
        /// <summary>https://gist.github.com/给你的ID</summary>
        const string gistID = "2dba9dc3384c1a2e1b6d3299052f317a";

        [MenuItem(topItem + "/Setup/Load New Manifest")]
        static async void LoadNewManifest() => await Packages.ReplacePaackagesFromGist(id: gistID );
        [MenuItem(topItem + "/Setup/输入系统")]
        static void AddNewInputSystem() => Packages.InstallUnitPackage("inputsystem");
        [MenuItem(topItem + "/Setup/后期处理")]
        static void AddPostPocessing() => Packages.InstallUnitPackage("postprocessing");
        [MenuItem(topItem + "/Setup/摄像机")]
        static void AddCinemachine() => Packages.InstallUnitPackage("cinemachine");



        #endregion

    }

    public static class Packages
    {
        /// <summary>你的github账户名字</summary>
        const string githubName = "WarbleWavelet";

        /// <summary>拿到manifest,进行包的下载</summary>
        public static async Task ReplacePaackagesFromGist( string id, string user= githubName)
        {
            var url = GetGistUrl(id, user);
            var contents = await GetContents(url);
            ReplacePackageFile(contents);
                
        }
        /// <summary>https://gist.github.com/ 填写你精简后的manifest得到的来宾ID</summary>
        private static string GetGistUrl(string id, string user = githubName)
        {
           return $"https://gist.githubusercontent.com/{user}/{id}/raw";
        }

        /// <summary>下载</summary>
        public static async Task<string> GetContents(string url)
        {
            var client = new HttpClient();
            var response = await client.GetAsync(url);
            var content = await response.Content.ReadAsStringAsync();
            return content;

        }

        /// <summary>覆盖本地manifest.json</summary>
        public static void ReplacePackageFile(string contents)
        {
            var existing = Path.Combine(Application.dataPath, "../Packages/manifest.json");
            File.WriteAllText(existing, contents);
            UnityEditor.PackageManager.Client.Resolve();
        }
        /// <summary>相当于在包管理器安装包</summary>
        internal static void InstallUnitPackage(string packageName)
        {
            UnityEditor.PackageManager.Client.Add($"com.unity.{packageName}") ;
        }
    }
}

效果 下载Cinemachine

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值