C#mainplayer.cs代码如下:
using JetBrains.Annotations;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//引入
using Assets.Creator_Kit___Beginner_Code.Scripts.myclass;
public class MainPlayer : MonoBehaviour
{
// Start is called before the first frame update
//单独调用
public Cat cat0;
public Dog dog;
void Start()
{
cat0.Print();
cat0.Perr();
dog.Perr();
List<Cat> cats = new List<Cat>();
for (int i = 1; i < 100; i++) {
cats.Add(new Cat($"这是第{i}只猫",i));
}
foreach (Cat cat in cats) {
cat.Perr();
cat.Print();
}
}
// Update is called once per frame
}
//创建Cat类
[Serializable]
public class Cat{
public string catname;
public int catage;
//构造方法
public Cat(string catname, int catage)
{
this.catname = catname;
this.catage = catage;
}
public void Print() {
Debug.Log($"这只猫的名字叫{catname},它今年{catage}岁了!");
}
public void Perr()
{
Debug.Log($"{catname},一直在不停的喵喵叫。。。。。");
}
}
//
c# Dog.cs代码:
using System;
using System.Collectns.Generic;
using System.Diagnostics;
using System.Linq;
using UnityEngine;
using System.Text;
using System.Threading.Tasks;
namespace Assets.Creator_Kit___Beginner_Code.Scripts.myclass
{
[Serializable]
public class Dog
{
public string name;
public int age;
public Dog(string name, int age) {
this.name = name;
this.age = age;
}
public void Perr() {
UnityEngine.Debug.Log($"这只狗的名字叫{name},今年{age}岁了。");
}
}
}
单独调用:
运行结果: