본문 바로가기
개발/C#

유니티 C# 자주 사용하는 연산자 Operator 모음

by SPNK 2022. 7. 13.
반응형
  • 산술 연산자
using UnityEngine;

public class Example : MonoBehaviour
{
    public int a = 10;
    public int b = 5;
    void Awake()
    {
        //더하기 연산자
        Debug.Log(a + b); // 15

        //빼기 연산자
        Debug.Log(a - b); // -5

        //곱하기 연산자
        Debug.Log(a * b); // 50

        //나누기 연산자
        Debug.Log(a / b); // 2
    
        //나머지 연산자
        b = 3;
        Debug.Log(a % b); // 1
        
        //할당 연산자
        a++; // a = 11

        a--; // a = 10
    }
}

 

 

  • 할당 연산자
using UnityEngine;

public class Example : MonoBehaviour
{
    public int a = 10;
    public int b = 5;
    void Awake()
    {
        //대입 연산자
        b = a; // b = 10


        //할당 연산자
        a += 1; 
        // a = a + 1 
        // a = 11

        a -= 1; 
        // a = a -1 
        // a = 10

        a *= 2; 
        // a = a * 2 
        // a= 20

        a /= 2; 
        // a = a / 2 
        // a = 10

        a %= 3; 
        // a = a % 3
        // a = 1
    }
}

 

 

  • 논리 연산자
using UnityEngine;

public class Example : MonoBehaviour
{
    public int a = 10;
    public int b = 5;
    void Awake()
    {
        //대입 연산자
        if(a == b)
        {
            Debug.Log("A와 B가 같습니다.");
        }
        else
        {
            Debug.Log("A와 B가 같지 않습니다.");
        }

        //같지않음 연산자
        if(a != b)
        {
            Debug.Log("A와 B가 다릅니다.");
        }
        else
        {
            Debug.Log("A와 B가 다릅니다.");
        }

        //AND 연산자
        if(a == 10 && b == 5)
        {
            Debug.Log("A는 10이고 b는 5가 맞습니다.");
        }
        else
        {
            Debug.Log("둘중 하나가 틀립니다.");
        }

        //OR 연산자
        if(a == 10 || b == 5)
        {
            Debug.Log("A는 10이거나 b가 5입니다.");
        }
        else
        {
            Debug.Log("둘다 틀립니다.");
        }
    }
}

참고할만한 글

 

 

유니티 C# 디버그 로그 종류 Debug.Log 간단 사용법

코드 작성 using UnityEngine; public class Example : MonoBehaviour { void Awake() { Debug.Log("회색 디버그 로그"); Debug.LogWarning("노란색 디버그 로그"); Debug.LogError("빨간색 디버그 로그"); } }

parksh3641.tistory.com

 

 

유니티 C# 비속어 필터 적용 inputfield 간단 사용법

Assets / Resoures / BadWord.txt 준비 코드 작성 using System.Collections; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; using UnityEngine; using UnityEngine.UI; public class Example : MonoBehaviour { public I

parksh3641.tistory.com

 

반응형

댓글