C# interface

interface IPoint
{
int Px
{
get; set;
}
int Py
{
get; set;
}
}

class ReversePoint : IPoint
{
int x;
int y;

public ReversePoint(int x, int y)
{
    this.x = x;
    this.y = y;
}

public int Px
{
    get { return -x; }
    set { x = value; }
}

// Implementing Py similarly to Px
public int Py
{
    get { return -y; } // Return the negative of y
    set { y = value; } // Set y directly
}

}

class MainClass
{
public static void DisplayPoint( IPoint point)
{
Console.WriteLine(“x={0},y={1}”, point.Px, point.Py);
}

static void Main()
{
    ReversePoint p1 = new ReversePoint(-12, -300);

    Console.WriteLine(p1.Px);
    Console.WriteLine(p1.Py);

    ReversePoint p2 = new ReversePoint(12, 300);

    //プロパティの参照
    DisplayPoint(p2);
}

}

ポケモン

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
public float moveSpeed;
public LayerMask solidObjectsLayer;
public LayerMask grassLayer;

private bool isMoving;
private Vector2 input;

private Animator animator;

private void Awake()
{
    animator = GetComponent<Animator>();
}
private void Update()
{
       if (!isMoving)
        {
        input.x = Input.GetAxisRaw("Horizontal");
        input.y = Input.GetAxisRaw("Vertical");

        //remove diagonal movement
        if (input.x != 0) input.y = 0;

        if(input != Vector2.zero)
        {
            animator.SetFloat("moveX", input.x);
            animator.SetFloat("moveY", input.y);
            var targetPos = transform.position;
            targetPos.x += input.x;
            targetPos.y += input.y;

            if(IsWalkable(targetPos))
            StartCoroutine(Move(targetPos));
        }
    }

    animator.SetBool("isMoving", isMoving);
}
IEnumerator Move(Vector3 targetPos)
{
    isMoving = true;

    while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon)
    {
        transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
        yield return null;
    }
    transform.position = targetPos; // 目標位置に合わせて最終位置を設定

    isMoving = false;

    CheckForEncounters();
}

private bool IsWalkable(Vector3 tagetPos)
{
    if(Physics2D.OverlapCircle(tagetPos, 0.2f, solidObjectsLayer) != null)
    {
        return false;
    }

    return true;
}

private void CheckForEncounters()
{
    if(Physics2D.OverlapCircle(transform.position, 0.2f, grassLayer) != null)
    {
        if(Random.Range(1, 101) <= 10)
        {
            Debug.Log("野生のポケモンに遭遇した");
        }
   
}

}

PlayerController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = “ポケモン”, menuName = “ポケモン/新しいポケモンを作成する”)]
public class PokemonBase : ScriptableObject
{
[SerializeField] string name;

[TextArea]
[SerializeField] string description;

[SerializeField] Sprite frontSprite;
[SerializeField] Sprite backSprite;

[SerializeField] PokemonType type1;
[SerializeField] PokemonType type2;

//Base Stats
[SerializeField] int maxHp;
[SerializeField] int attack;
[SerializeField] int defense;
[SerializeField] int spAttack;
[SerializeField] int spDefense;
[SerializeField] int speed;

}

public enum PokemonType
{
None,
Normal,
Fire,
Water,
Electric,
Grass,
Ice,
Fighting,
Poison,
Ground,
Flying,
Psychic,
Bug,
Rock,
Ghost,
Dragon
}

PokemonBase

C# Talk.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Talk : MonoBehaviour
{
public GameObject panel;
public Text txt;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}

void OnTriggerEnter(Collider col)
{
    panel.SetActive(true);
}

}

C# オーバーライド

using System;

//基本クラス
class Music
{
public virtual void BaseInfo()
{
Console.WriteLine(“Music”);
}
}

//派生クラス
class Song : Music
{
public override void BaseInfo()
{
Console.WriteLine(“Song”);
}
}

//派生クラス
class Music2 : Music
{
new public void BaseInfo()
{
Console.WriteLine(“Music2”);
}
}

class MainClass
{
static void Main()
{
Song s = new Song();
s.BaseInfo();

    Music2 m2 = new Music2();
    m2.BaseInfo();

    Music m = new Music();
    m.BaseInfo();
}

}

C# 変数

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test3 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
int a;

    float b;

    string c;

    bool d;

    a = 1;
    b = 0.01f;
    c = "勇者の登場";
    d = true;

    //コンソールに表示
    Debug.Log(a);
    Debug.Log(b);
    Debug.Log(c);
    Debug.Log(d);
}

}

C# Unity Test

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class TestScript : MonoBehaviour

{

    // Start is called before the first frame update

    void Start()

    {

       string a;

       string b;

       a = “真理”;

       b = “りんご”;

       Debug.Log(a + “は” + b + “が好きです。”);

       a = “マリアンヌ”;

       b = “メロン”;

       Debug.Log(a + “は” + b + “好きです。”);

    }

    // Update is called once per frame

    void Update()

    {

        //Debug.Log(“連続表示”);

    }

}