[英]How to generate a non-existing C# script programatically in Unity3D
如何生成或创建不存在的脚本(最好是C#)?
为我的对象的动画创建方法总是很耗时又累。
因此,我计划创建一个自定义Editor
,在其中我可以引用带有Animation
组件的GameObject
,输入要生成的脚本的名称,然后单击按钮以使用播放该对象动画的方法自动生成C#脚本。
我想知道是否有办法制作一个字符串并将所需的代码分配给该字符串,然后将其转换为C#脚本。
我已经阅读过AssetDatabase
和Monoscript
有关创建资产的内容,但是仍然没有运气。
将生成的脚本附加到GameObject
没问题。
谢谢。
[更新]附加信息
因此,基于此组件,我想生成一个C#脚本,其中包含播放此类动画的方法。
using UnityEngine;
using System.Collections;
public class Level3TerrainB : MonoBehaviour {
private string animationPrefix = "arm_Root_PlatformB|anim_";
public void AnimatePressButtonA() {
animation.Blend (animationPrefix + "PressButtonA");
}
public void AnimateUnpressButtonA() {
animation.Blend (animationPrefix + "UnpressButtonA");
}
public void AnimatePressButtonB() {
animation.Blend (animationPrefix + "PressButtonB");
}
public void AnimateUnpressButtonB() {
animation.Blend (animationPrefix + "UnpressButtonB");
}
public void AnimateUpButtonB() {
animation.Blend (animationPrefix + "UpButtonB");
}
public void AnimateDownButtonB() {
animation.Blend (animationPrefix + "DownButtonB");
}
public void AnimatePressButtonC() {
animation.Blend (animationPrefix + "PressButtonC");
}
public void AnimateUnpressButtonC() {
animation.Blend (animationPrefix + "UnpressButtonC");
}
public void AnimateUpButtonC() {
animation.Blend (animationPrefix + "UpButtonC");
}
public void AnimateDownButtonC() {
animation.Blend (animationPrefix + "DownButtonC");
}
public void AnimatePressButtonD() {
animation.Blend (animationPrefix + "PressButtonD");
}
public void AnimateUnpressButtonD() {
animation.Blend (animationPrefix + "UnpressButtonD");
}
public void AnimateLeftUpPulleyA() {
animation.Blend (animationPrefix + "LeftUpPulleyA");
}
public void AnimateRightUpPulleyA() {
animation.Blend (animationPrefix + "RightUpPulleyA");
}
public void AnimateLeftUpPulleyB() {
animation.Blend (animationPrefix + "LeftUpPulleyB");
}
public void AnimateRightUpPulleyB() {
animation.Blend (animationPrefix + "RightUpPulleyB");
}
}
我不想一遍又一遍地进行硬编码。.因为我还有许多其他层次可以进行动画处理。
仅使用一种方法并将动画名称作为参数会更容易吗?
就像是:
public void Animate(string animationName)
{
animation.Blend (animationPrefix + animationName);
}
因此,我只是创建了一个脚本来生成代码字符串,将其打印出来,然后复制并粘贴到新脚本中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.