LayerMask

Unity3D 2019. 5. 1. 09:54
반응형

https://answers.unity.com/questions/8715/how-do-i-use-layermasks.html

 

How do I use layermasks? - Unity Answers

 

answers.unity.com

https://docs.unity3d.com/ScriptReference/LayerMask.html

 

Unity - Scripting API: LayerMask

A GameObject can use up to 32 LayerMasks supported by the Editor. The first 8 of these Layers are specified by Unity; the following 24 are controllable by the user. Bitmasks represent the 32 Layers and define them as true or false. Each bitmask describes w

docs.unity3d.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TestLayerMask : MonoBehaviour
{
    // Start is called before the first frame update
    // 인스턴스가 활성화된 경우에만 호출
    void Start()
    {
        //32비트 레이어 마스크 지원 
        int floorLayer = LayerMask.NameToLayer("Floor");
        string floorLayerName = LayerMask.LayerToName(floorLayer);
        int floorLayerMask1 = LayerMask.GetMask(floorLayerName);
        int floorLayerMask2 = 1 << floorLayer;
        string strBinaryFloorLayer = Convert.ToString(floorLayerMask1, 2);
 
        //floorLayer: 8, floorLayerName: Floor, floorLayerMask1: 256, floorLayerMask2: 256
        Debug.LogFormat("floorLayer: {0}, floorLayerName: {1}, floorLayerMask1: {2}, floorLayerMask2: {3}", floorLayer, floorLayerName, floorLayerMask1, floorLayerMask2);
 
        //100000000
        //0000 0000 0000 0000 0000 0001 0000 0000
        Debug.LogFormat("binaryFloorLayer: {0}", strBinaryFloorLayer);
 
        int shootableLayer = LayerMask.NameToLayer("Shootable");
        string shootableLayerName = LayerMask.LayerToName(shootableLayer);
        int shootableLayerMask1 = LayerMask.GetMask(shootableLayerName);
        int shootableLayerMask2 = 1 << shootableLayer;
        string strBinaryShootableLayer = Convert.ToString(shootableLayerMask2, 2);
 
        //shootableLayer: 9, shootableLayerName: Shootable, shootableLayerMask1: 512, shootableLayerMask2: 512
        Debug.LogFormat("shootableLayer: {0}, shootableLayerName: {1}, shootableLayerMask1: {2}, shootableLayerMask2: {3}", shootableLayer, shootableLayerName, shootableLayerMask1, shootableLayerMask2);
 
        //100000000
        //0000 0000 0000 0000 0000 0010 0000 0000
        Debug.LogFormat("strBinaryShootableLayer: {0}", strBinaryShootableLayer);
 
        //Or
        //768
        //1100000000
        //0000 0000 0000 0000 0000 0011 0000 0000      
        var orLayerMask = floorLayerMask1 | shootableLayerMask1;
        Debug.LogFormat("orLayerMask: {0}", orLayerMask);
        string strBinaryOrLayer = Convert.ToString(orLayerMask, 2);
        Debug.LogFormat("strBinaryOrLayer: {0}", strBinaryOrLayer);
 
        //Not
        var notLayerMask = ~orLayerMask;
        //strBinaryNotLayer: -769
        Debug.LogFormat("notLayerMask: {0}", notLayerMask);
        string strBinaryNotLayer = Convert.ToString(notLayerMask, 2);
        //strBinaryNotLayer: 11111111111111111111110011111111
        Debug.LogFormat("strBinaryNotLayer: {0}", strBinaryNotLayer);
 
 
        var allLayer = -1;
        string allBinaryLayer = Convert.ToString(allLayer, 2);
        //allBinaryLayer: 11111111111111111111111111111111
        Debug.LogFormat("allBinaryLayer: {0}", allBinaryLayer);
 
 
        //And
        var andLayerMask = floorLayerMask1 & allLayer;
        //andLayerMask: 256
        Debug.LogFormat("andLayerMask: {0}", andLayerMask);
        string strBinaryAndLayer = Convert.ToString(andLayerMask, 2);
        //strBinaryAndLayer: 100000000
        //strBinaryAndLayer: 0000 0000 0000 0000 0000 0001 0000 0000
        /*
         * 
         *  1111 1111 1111 1111 1111 1111 1111 1111
            0000 0000 0000 0000 0000 0001 0000 0000
            ---------------------------------------
            0000 0000 0000 0000 0000 0001 0000 0000
         */
 
        Debug.LogFormat("strBinaryAndLayer: {0}", strBinaryAndLayer);
 
    }
}
 
 
 
반응형

'Unity3D' 카테고리의 다른 글

[ml-agents] Hyperparameters  (0) 2019.05.19
Tower Defense Template (분해)  (0) 2019.05.03
world, screen, viewport  (0) 2019.04.23
Quaternion.LookRotation  (0) 2019.04.23
(unity) find child recursively  (1) 2019.04.21
: