服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - unity学习教程之定制脚本模板示例代码

unity学习教程之定制脚本模板示例代码

2022-03-07 13:26禹泽鹏鹏 C#

这篇文章主要给大家介绍了关于unity学习教程之定制脚本模板的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1、unity的脚本模板

新版本unity中的c#脚本有三类,第一类是我们平时开发用的c# script;第二类是testing,用来做单元测试;第三类是playables,用作timeline中管理时间线上每一帧的动画、声音等。我们点击创建脚本时,会自动生成unity内置的一套模板:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using system.collections;
using system.collections.generic;
using unityengine;
 
public class newbehaviourscript : monobehaviour {
 
 // use this for initialization
 void start () {
  
 }
 
 // update is called once per frame
 void update () {
  
 }
}

如果我们开发时使用的框架有明显的一套基础模板, 那为项目框架定制一套模板会很有意义,这样可以为我们省去编写重复代码的时间。这里介绍两种方法。

2、修改默认脚本模板

打开unity安装目录,比如d:\unity2018\editor\data\resources\scripttemplates,unity内置的模板脚本都在这里,那么可以直接修改这里的cs文件,比如我们将81-c# script-newbehaviourscript.cs.txt文件修改为如下,那下次创建c# script时模板就会变成这样:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
////////////////////////////////////////////////////////////////////
//       _ooooo_        //
//       o8888888o        //
//       88" . "88        //
//       (| ^_^ |)        //
//       o\ = /o        //
//      ____/`---'\____       //
//     .' \\|  |// `.       //
//     / \\||| : |||// \      //
//     / _||||| -:- |||||- \      //
//     | | \\\ - /// | |      //
//     | \_| ''\---/'' | |      //
//     \ .-\__ `-` ___/-. /      //
//    ___`. .' /--.--\ `. . ___      //
//    ."" '< `.___\_<|>_/___.' >'"".     //
//   | | : `- \`.;`\ _ /`;.`/ - ` : | |     //
//   \ \ `-. \_ __\ /__ _/ .-` / /     //
//  ========`-.____`-.___\_____/___.-`____.-'========   //
//       `=---='        //
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  //
//   佛祖保佑  永不宕机  永无bug     //
////////////////////////////////////////////////////////////////////
 
 
 
using system.collections;
using system.collections.generic;
using unityengine;
 
public class #scriptname# : monobehaviour {
 
 // use this for initialization
 void start () {
  #notrim#
 }
 
 // update is called once per frame
 void update () {
  #notrim#
 }
}

3、拓展脚本模板

上面讲的第一种方法直接修改了unity的默认配置,这并不适应于所有项目,这里第二种方法会更有效,可以针对不同的项目和框架创建合适的脚本模板。

首先,先创建一个文本文件mytemplatescript.cs.txt作为脚本模板,并将其放入unity project的editor文件夹下,模板代码为:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using system.collections;
using system.collections.generic;
using unityengine;
 
public class mynewbehaviourscript : monobase {
 
 //添加事件监听
 protected override void addmsglistener()
 {
 
 }
 
 //处理消息
 protected override void handlemsg(msgbase msg)
 {
  switch (msg.id)
  {
   default:
    break;
  }
 }
 
}

我们使用时,需要在project视图中右击->create->c# framescript 创建脚本模板,因此首先要创建路径为assets/create/c# framescript的menuitem,点击创建脚本后,需要修改脚本名字,因此需要在拓展编辑器脚本中继承endnameeditaction来监听回调,最终实现输入脚本名字后自动创建相应的脚本模板。

unity学习教程之定制脚本模板示例代码

代码如下,将这个脚本放入editor文件夹中:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using unityeditor;
using unityengine;
using system;
using system.io;
using unityeditor.projectwindowcallback;
using system.text;
using system.text.regularexpressions;
 
public class createtemplatescript {
 
  //脚本模板路径
  private const string templatescriptpath = "assets/editor/mytemplatescript.cs.txt";
 
  //菜单项
  [menuitem("assets/create/c# framescript", false, 1)]
  static void createscript()
  {
    string path = "assets";
    foreach (unityengine.object item in selection.getfiltered(typeof(unityengine.object),selectionmode.assets))
    {
      path = assetdatabase.getassetpath(item);
      if (!string.isnullorempty(path) && file.exists(path))
      {
        path = path.getdirectoryname(path);
        break;
      }
    }
    projectwindowutil.startnameeditingifprojectwindowexists(0, scriptableobject.createinstance<createscriptasset>(),
    path + "/mynewbehaviourscript.cs",
    null, templatescriptpath);
 
  }
  
}
 
 
class createscriptasset : endnameeditaction
{
  public override void action(int instanceid, string newscriptpath, string templatepath)
  {
    unityengine.object obj= createtemplatescriptasset(newscriptpath, templatepath);
    projectwindowutil.showcreatedasset(obj);
  }
 
  public static unityengine.object createtemplatescriptasset(string newscriptpath, string templatepath)
  {
    string fullpath = path.getfullpath(newscriptpath);
    streamreader streamreader = new streamreader(templatepath);
    string text = streamreader.readtoend();
    streamreader.close();
    string filenamewithoutextension = path.getfilenamewithoutextension(newscriptpath);
    //替换模板的文件名
    text = regex.replace(text, "mytemplatescript", filenamewithoutextension);
    bool encodershouldemitutf8identifier = true;
    bool throwoninvalidbytes = false;
    utf8encoding encoding = new utf8encoding(encodershouldemitutf8identifier, throwoninvalidbytes);
    bool append = false;
    streamwriter streamwriter = new streamwriter(fullpath, append, encoding);
    streamwriter.write(text);
    streamwriter.close();
    assetdatabase.importasset(newscriptpath);
    return assetdatabase.loadassetatpath(newscriptpath, typeof(unityengine.object));
  }
 
}

然后,在project中,点击创建c# framescript,输入脚本名字,对应的脚本就已经创建好了

unity学习教程之定制脚本模板示例代码

4、总结

上面介绍了两种方案,第一种适合玩玩,第二种方法显然逼格高一些,为不同的项目和框架定制一套脚本模板,可以让我们少写一些重复代码。按照上面介绍的方法,我们同样可以修改和拓展testing、playables的脚本模板,甚至shader,我们也可以定制模板。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:http://www.cnblogs.com/IAMTOM/p/10156148.html

延伸 · 阅读

精彩推荐
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11