Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement C # Code Generator Application Service for Unity by Python

Shulou Source: shulou.com Published: 2022-06-03 07:57:56 10月02日 Update

This article will explain in detail how Python implements the C# code generator application for Unity. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Development goal: to achieve the automatic generation of scripts hung by Little Red Riding Hood. The following figure is the ultimate goal of the generation.

The purpose of this project is to read information such as character scenes from json, so in order to better judge whether the property used is needed, set it to bool type, False means to comment out this kind of property in the c # code, and True represents to use this attribute (the property is temporarily set to)

Timer = True # timer speed = False # Speed IsTrigger = True # trigger start_point = True # start position localScale = True # start size main program specific python code is as follows: from string import Templateclass BuildData: def Init (self): # initialize all kinds of $Timer = False IsTrigger = True start_point = True localScale = True # output a.cs file filePath = 'a.cs' class_file = open (filePath 'w') # mycode is used to store the generated code mycode = [] # load template file template_file = open ('TMPL1.tmpl',' rb') template_file = template_file.read (). Decode ('utf-8') tmpl = Template (template_file) # # template replacement # 1. Need to determine whether or not to use the template Comment out if (Timer): TimerContent =''else: TimerContent =' /'if (speed): speedContent =''else: speedContent =' /'if (IsTrigger): IsTriggerContent =''else: IsTriggerContent = If (start_point): start_pointcontent=''else: start_pointcontent=' /'if (localScale): localScalecontent=' 'else: localScalecontent='///' # 2. Fixed template values are replaced by mycode.append (tmpl.safe_substitute (TimerContent=TimerContent, speedContent=speedContent, IsTriggerContent=IsTriggerContent, start_pointcontent=start_pointcontent, localScalecontent=localScalecontent, role='Small_red_hat', x starting starting pointboxes 12 minutes, yearly startling pointpoints equal mus2 values, zonal starting pointboxes 0' Xtriple scaleholders 0.45fags, yawns scalebacks 0.5f' ) # write the code to the file class_file.writelines (mycode) class_file.close () print ('code generated') if _ _ name__ = ='_ _ main__': build = BuildData () build.Init () the TMPL file set is as follows: using System.Collections Using System.Collections.Generic;using UnityEngine;public class ${role}: MonoBehaviour {${TimerContent} public float Timer; / / set a Timer ${speedContent} public float speed; / / speed ${IsTriggerContent} public bool IsTrigger / set a trigger void Start () {/ / the start_point of ${role} ${start_pointcontent} transform.position = new Vector3 (${x_start_point}, ${y_start_point}, ${z_start_point}); / / the scale of ${role} ${localScalecontent} transform.localScale = new Vector3 (${x_scale}, ${y_scale}, ${z_scale}) } void Update () {/ / Timer countdown ${TimerContent} Timer + = Time.deltaTime; / / when to move ${TimerContent} if (Timer > = 2f & & Timer 3.5f) {IsTrigger = false;} / / the speed of ${role} ${IsTriggerContent} if (IsTrigger) {transform.Translate (- 0.04f, 0,0);} the automatically generated c # code is shown as follows: using System.Collections Using System.Collections.Generic;using UnityEngine;public class Small_red_hat: MonoBehaviour {public float Timer; / / set a Timer / / public float speed; / / speed public bool IsTrigger; / / set a trigger void Start () {/ / the start_point of Small_red_hat transform.position = new Vector3 (12,-2,0) / / the scale of Small_red_hat transform.localScale = new Vector3 (0.45fmai 0.5f, 1);} void Update () {/ / Timer countdown Timer + = Time.deltaTime; / / when to move if (Timer > = 2f & & Timer 3.5f) {IsTrigger = false } / / the speed of Small_red_hat if (IsTrigger) {transform.Translate (- 0.04f, 0,0);}

If you look closely at the generated results, the code is basically the same as the target generated code. (comments can only be edited in English for the time being. Then put the generated code in unity and observe how it works

Before running:

After running:

This is the end of this article on "how Python implements the C# code generator application service for Unity". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Tags: Code generation properties files templates comments goals articles runs application services generators caches applications services more automatic generation caching observation start good Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker OPPO Reno macOS Shulou Technology Xiaomi