0

How to generate or create a non existing script (preferably C#)?

It is always time consuming and tiring for me to create the methods for my object's animations.

So I am planning to create a custom Editor where I can just refer the GameObject with Animation component, enter a name for the script to be generated then click a button to automatically generate a C# script with methods that plays the animations of that object.

I was wondering if there's a way to make a string and assign the code needed to that string then turn it into a C# script.

I have read on AssetDatabase and Monoscript about create an Asset but still no luck.

There's no problem with attaching the generated script to a GameObject.

Thank you.

[Update] Additional Information This is my animations

So based to this component, I want to generate a C# script that has the methods of playing each animation like this..

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");
    }

}

I don't want to hard code it again, and again.. because I have many other levels to animate to.

6
  • Do you really need to generate a new script, or should you just make your existing script more generic? Commented Aug 20, 2014 at 10:02
  • Are you actually talking about editor scripts or plugins? You can't generate a new script with Unity, you will need to do a mini plugin. Commented Aug 20, 2014 at 10:04
  • @Bart I think I really need to because my animation names and objects are not similar to each other. But if you can give suggestions I'll try it. Commented Aug 20, 2014 at 10:07
  • @Snake I am talking about creating a Monobehavior script. May I know how or what kind of a mini plugin? Commented Aug 20, 2014 at 10:09
  • Well, and example of what you're looking to generate (and how it changes with varying input) would certainly help @MarkVizcarra. Commented Aug 20, 2014 at 10:13

3 Answers 3

2

Wouldn't it be much easier to just use one method and give the animation name as parameter?

Something like:

public void Animate(string animationName) 
{
    animation.Blend (animationPrefix + animationName);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Yes. I tried that but for my workflow and the type of game I am doing. It does not fit.
@MarkVizcarra Why would it make any difference? Instead of calling for example: AnimatePressButtonC() you will call Animate("PressButtonC") Where does it interfere with your workflow? Can you please provide an example to show why you cannot use this solution. It will give a better impression of what you want to accomplish. Thank you.
To be honest one of the scripts I use takes an input in the inspector of a script, and puts the method name. It doesn't support methods with arguments so I try to work around it.
As far as I understand your situation you have a Behaviour Script you use to map inputs like buttons,... to method names. Do you have a fixed amount of inputs? In that case you could do one method per input and call Animate(animationName) based on the actual level from the input method using switch.
0

You could use the T4 templating -> http://msdn.microsoft.com/en-us/library/bb126445.aspx

Comments

0

So I just created a script to generate the string of codes, print it out, then copy and paste to a new script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.