LINQ를 사용한 JSON 쿼리(Query): LINQ 쿼리를 사용하여 JSON 데이터를 쉽게 쿼리하고 필터링할 수 있습니다.
JSON 스키마(Validation): JSON 데이터가 주어진 스키마에 부합하는지 확인할 수 있습니다.
JSON 파싱(Parsing): JSON 문자열을 해석하고 객체로 변환합니다.
JSON 변환(Transform): JSON 데이터를 다른 형식으로 변환합니다.
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class HelmMain : MonoBehaviour
{
void Start()
{
TextAsset asset = Resources.Load<TextAsset>("helm_data");
string json = asset.text;
Debug.Log(json);
HelmData data = JsonConvert.DeserializeObject<HelmData>(json);
//Debug.Log(data);
Debug.LogFormat("{0} {1} {2} {3}", data.name, data.min_damage, data.max_damage, data.item_type);
//저장
//직렬화 객체 생성
HelmInfo info = new HelmInfo(data.min_damage, data.max_damage);
//직렬화 시작(객체 ->문자열)
string serializedJson = JsonConvert.SerializeObject(info);
//파일로 저장
//Application.persistentDataPath : 플랫폼 OS에 따라 경로를 자동으로 잡아줌
string path = Application.persistentDataPath + "/helm_info.json";
Debug.Log(path);
//문자열을 파일로 저장
File.WriteAllText(path, serializedJson); //경로, 문자열
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HelmData
{
public string name;
public int min_damage;
public int max_damage;
public string item_type;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HelmInfo
{
public int min_damage;
public int max_damage;
public HelmInfo(int min_damage, int max_damage)
{
this.min_damage = min_damage;
this.max_damage = max_damage;
}
}
실행을 시켜보면
1. 데이터를 문자열을 출력
2. 데이터를 역직렬화 후 데이터를 잘 받고 있는지 확인
3. Helminfo클래스를 메인에서 불러온 후 생성자를 생성한뒤 지정한 경로 위치로 생성후 저장 => 그 위치 출력
그것을 하기 전에 제일 기초이자 중요한 GameObject.onClick.AddListener()를 다루려 한다.
Button1을 클릭하면 콘솔에 찍힌다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Main : MonoBehaviour
{
[SerializeField] private Button btn;
void Start()
{
btn.onClick.AddListener(() =>
{
Debug.Log("button clicked!");
});
}
};
Button1을 클릭하면 2, 3번이 사라진다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Main : MonoBehaviour
{
[SerializeField] private Button btn1;
[SerializeField] private Button btn2;
[SerializeField] private Button btn3;
void Start()
{
btn1.onClick.AddListener(() => {
this.btn2.gameObject.SetActive(false);
this.btn3.gameObject.SetActive(false);
});
}
}
Button1을 클릭하면 2, 3이 사라지는데
Button2, Butto3을 배열(btns)로 만들어서 할당하였다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Main : MonoBehaviour
{
[SerializeField] private Button btn1;
[SerializeField] private Button[] btns;
void Start()
{
btn1.onClick.AddListener(() => {
for (int i = 0; i < this.btns.Length; i++)
{
Button btn = btns[i];
btn.gameObject.SetActive(false);
}
});
}
}
Button1, Button2를 btns(배열)에 할당하였고
Button3을 누를시 배열이 사라지고 Button4를 누를시 배열이 다시 생기게 작성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Main : MonoBehaviour
{
[SerializeField] private Button btn3;
[SerializeField] private Button btn4;
[SerializeField] private Button[] btns; //btn1, btn2
void Start()
{
btn3.onClick.AddListener(() =>
{
Debug.Log("버튼3 눌렸습니다.");
for (int i = 0; i < this.btns.Length; i++)
{
Button btn = btns[i];
btn.gameObject.SetActive(false);
}
});
btn4.onClick.AddListener(() =>
{
Debug.Log("버튼4 눌렸습니다.");
for (int i = 0; i < this.btns.Length; i++)
{
Button btn = btns[i];
btn.gameObject.SetActive(true);
}
});
}
}
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Assertions.Comparers;
using UnityEngine.SceneManagement;
public class CatController : MonoBehaviour
{
[SerializeField] private Rigidbody2D rbody;
[SerializeField] private float moveForce = 100f;
[SerializeField] private float jumpForce = 680f;
[SerializeField]
private ClimbCloudGameDirector gameDirector;
private Animator anim;
private bool hasSpace = false;
private void Start()
{
//this.gameObject.GetComponent<Animation>();
anim = GetComponent<Animator>();
//this.gameDirector = GameObject.Find("GameDirector").GetComponent<ClimbCloudGameDirector>();
//this.gameDirector = GameObject.FindAnyObjectByType<ClimbCloudGameDirector>();
}
void Update()
{
//스페이스바를 누르면
if (Mathf.Abs(rbody.velocity.y) < 0.01f)
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (!hasSpace)
{
//힘을 가한다
this.rbody.AddForce(this.transform.up * this.jumpForce);
//this.rbody.AddForce(Vector3.up * this.force);
hasSpace = false; // 다시 점프할 수 있도록 허용
}
}
}
// -1, 0, 1 : 방향
int dirX = 0;
//왼쪽화살표키를 누르고 있는 동안에
if (Input.GetKey(KeyCode.LeftArrow))
{
dirX = -1;
}
if (Input.GetKey(KeyCode.RightArrow))
{
dirX = 1;
}
// Debug.Log(dirX); //방향 -1, 0, 1
//스케일 X를 변경 하는데 키가 눌렸을 때만
//키가 눌렸을때만 = (dirX != 0)
if (dirX != 0)
{
this.transform.localScale = new Vector3(dirX, 1, 1);
}
//벡터의 곱
//Debug.Log(this.transform.right * dirX); //벡터3
//도전 ! : 속도를 제한하자
//velocity.x 가 3정도가 넘어가니깐 빨라지는거 같드라구...
if (Mathf.Abs(this.rbody.velocity.x) < 3)
{
this.rbody.AddForce(this.transform.right * dirX * moveForce);
}
this.anim.speed = (Mathf.Abs(this.rbody.velocity.x) / 2f);
this.gameDirector.UpdateVelocityText(this.rbody.velocity);
// Debug.Log(this.transform.position);
float clampX = Mathf.Clamp(this.transform.position.x, -2.39f, 2.35f);
Vector3 pos = this.transform.position;
pos.x = clampX;
this.transform.position = pos;
}
// Trigger 모드 일경우 충돌 판정을 해주는 이벤트 함수
private bool hasEntered = false;
private void OnTriggerEnter2D(Collider2D collision)
{
if (!hasEntered)
{
Debug.LogFormat("OnTriggerEnter2D: {0}", collision);
SceneManager.LoadScene("ClimbCloudClear");
hasEntered = true; // 한 번 이벤트가 발생하면 이 변수를 true로 설정하여 두 번 이상 호출되지 않도록 함
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEditor.SearchService;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ChangeScene : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
SceneManager.LoadScene("ClimbCloud");
Debug.Log("화면이 전환됨");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ClimbCloudGameDirector : MonoBehaviour
{
[SerializeField] private Text velocityText;
public void UpdateVelocityText(Vector2 velocity)
{
float velocityX = Mathf.Abs(velocity.x);
this.velocityText.text = velocityX.ToString();
}
}
콜라이더(Collider)컴포넌트는 물리 충돌 처리를 위한 오브젝트의 형태를 정의합니다. 콜라이더는 보이지 않는 요소이므로 오브젝트의 메시와 정확히 동일한 모양일 필요는 없으며, 실제로는 게임플레이 시에는 대략적인 근사치로도 크게 구분되지 않으며 더 효율적입니다.
가장 간단한(그리고 프로세서에 부하를 주지 않는) 콜라이더는기본콜라이더 타입입니다. 3D에서는박스 콜라이더,스피어 콜라이더,캡슐 콜라이더가 바로 이 타입입니다. 2D에서는박스 콜라이더 2D와써클 콜라이더 2D를 사용할 수 있습니다.복합 콜라이더를 만들기 위해 하나의 오브젝트에 위와 같은 콜라이더를 몇 개든 추가할 수 있습니다.