using System;
class array01
{
public static void Main()
{
int[] Weapon = new int[3];
Weapon[0] = 10;
Weapon[1] = 20;
Weapon[2] = 30;
// 宣言と同時に初期化
int[] myarray2 = new int[3] { 10, 20, 30 };
// 要素数を省略することも可能
int[] myarray3 = new int[] { 10, 20, 30 };
//別な方法
int[] myarray4;
myarray4 = new int[] { 10, 20, 30 };
}
}
タグ: C#
C# おにゃんこ大戦争
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharactorMove : MonoBehaviour
{
public enum TYPE
{
PLAYER,
ENEMY,
}
public TYPE type = TYPE.PLAYER;
float direction;
Vector3 pos;
bool isMove = true;
// Start is called before the first frame update
void Start()
{
switch(type)
{
case TYPE.PLAYER:
//Player時の処理
direction = -1;
break;
case TYPE.ENEMY:
//Enemyの時の処理
direction = 1;
break;
}
pos = new Vector3(direction, 0, 0);
}
// Update is called once per frame
void Update()
{
if(isMove)
{
transform.position += pos * Time.deltaTime;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
//敵にぶつかったら移動とめる
if(collision.gameObject.tag == "Enemy" && type == TYPE.PLAYER
|| collision.gameObject.tag == "Player" && type == TYPE.ENEMY)
{
isMove = false;
}
//攻撃をしはじめる
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == "Enemy" && type == TYPE.PLAYER
|| collision.gameObject.tag == "Player" && type == TYPE.ENEMY)
{
isMove = true;
}
}
}
C# 配列と構造体
using System;
struct Simple
{
public int Number;
public string Name;
public Simple(int n,string s)
{
Number = n;
Name = s;
}
}
class MainClass
{
static void Main()
{
Simple s1 = new Simple();
Console.WriteLine(s1.Number);
Console.WriteLine(s1.Name);
Simple s2 = new Simple(1, "testname");
Console.WriteLine(s2.Number);
Console.WriteLine(s2.Name);
Simple ss;
}
}
C# 配列
using System;
namespace array3
{
internal class Program
{
static void Main(string[] args)
{
string[] weekDays =
{ “sun”, “Mon”, “Tue” , “Wed”, “Thu”, “Fri” , “Sat”};
for(int i = 0; i < weekDays.Length; i++)
{
Console.WriteLine(weekDays[i]);
}
foreach(string s in weekDays)
{
Console.WriteLine(s);
}
int[] a = { 1, 2, 3 };
int sum = 0;
for(int i = 0; i < a.Length; i++)
{
sum += a[i];
}
Console.WriteLine(sum);
}
}
}
C# 配列
using System;
namespace array1
{
internal class Program
{
static void Main(string[] args)
{
int[] array = new int[10];
//代入
array[0] = 1;
array[1] = 2;
Console.WriteLine(array[0] + array[1]);
}
}
}
C# オーバーフロー
using System;
namespace overflow
{
internal class Program
{
static void Main(string[] args)
{
try
{
short a = short.MaxValue;
Console.WriteLine(a);
a++;
Console.WriteLine(a);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
C# throw文
using System;
namespace exception8
{
internal class Program
{
static void Main(string[] args)
{
try
{
try
{
int[] a = new int[3];
a[0] = 1;
a[5] = 2;
}
catch (Exception)
{
Console.WriteLine("最初の捕捉");
throw;
}
}
catch (Exception e)
{
Console.WriteLine("外側の捕捉");
Console.WriteLine(e.Message);
}
}
}
}
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);
}
}
