TextMesh









How do I use Text Mesh
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: ShowInfoOfPlayer.cs Copy
20     void Start()
21     {
22         if (font == null)
23         {
24             #if UNITY_3_5
25             font = (Font)FindObjectsOfTypeIncludingAssets(typeof(Font))[0];
26             #else
27             font = (Font)Resources.FindObjectsOfTypeAll(typeof(Font))[0];
28             #endif
29             Debug.LogWarning("No font defined. Found font: " + font);
30         }
31
32         if (tm == null)
33         {
34             textGo = new GameObject("3d text");
35             //textGo.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
36             textGo.transform.parent = this.gameObject.transform;
37             textGo.transform.localPosition = Vector3.zero;
38
39             MeshRenderer mr = textGo.AddComponent();
40             mr.material = font.material;
41             tm = textGo.AddComponent();
42             tm.font = font;
43             tm.anchor = TextAnchor.MiddleCenter;
44             if (this.CharacterSize > 0)
45             {
46                 tm.characterSize = this.CharacterSize;
47             }
48         }
49     }
File name: BlockScript.cs Copy
94  public void setBlockNumber(int blockNumber) {
95   this.blockNumber = blockNumber;
96   TextMesh textMesh = this.GetComponentInChildren();
97   Material blockTextMaterial = gameObject.transform.Find ("BlockText").gameObject.GetComponent().material;
98   MeshRenderer cube = gameObject.GetComponentInChildren();
99   Color cubeColor = cube.GetComponent().material.color;
100
101   //block doesn't exits
102   if(blockNumber == -2) {
103    cube.GetComponent().enabled = false;
104    textMesh.text = "";
105   }
106   //block is empty
107   else if (blockNumber == -1 ) {
108    cube.GetComponent().enabled = false;
109    cube.GetComponent().material.color = new Color(1, 1, 1, 0.2f);
110    textMesh.text = "";
111   }
112   //block has a value
113   else {
114    cube.GetComponent().enabled = true;
115    cube.material.color = this.getColor (blockNumber);
116    blockTextMaterial.SetColor ("_Color", new Color(1,1,1));
117    if(blockNumber == 0) blockTextMaterial.SetColor ("_Color", new Color(0.8f,0.8f,1));
118    textMesh.text = blockNumber.ToString();
119    transform.position = this.originalPosition;
120   }
121
122  }
File name: GameControllerScript.cs Copy
48  void Start (){
49
50   int x;
51   int y;
52   int z;
53   int axis;
54   yOffset = 1F;
55   Transform blockInstance;
56   BlockScript blockScript;
57   ConnectorScript connectorScript;
58   Transform connectorInstance;
59   TextMesh textMesh;
60
61   this.gameView = "menu";
62   this.options = this.gameObject.GetComponent ("OptionsScript") as OptionsScript;
63   this.timer = this.gameObject.GetComponent ("TimerScript") as TimerScript;
64   this.sizeGUI();
65
66   this.options.InitOptions();
67
68   //setup audio sources
69
70   AudioSource[] audioSources = GetComponents();
71   this.swipeAudioSource = audioSources[0];
72   this.collideAudioSource = audioSources[1];
73   this.collideAudioSource.clip = this.collideSound;
74
75   //instantiate the blocks and connectors and position them
76   for (x = 0; x <= 2; x++) {
77    for (y = 0; y <= 2; y++) {
78     for (z = 0; z <= 2; z++) {
79      //instantiate the block
80      blockInstance = Instantiate (block, new Vector3(x * this.scale, y * this.scale + this.yOffset, z * this.scale), Quaternion.identity) as Transform;
81      this.blocks[x,y,z] = blockInstance;
82      blockScript = blockInstance.gameObject.GetComponent("BlockScript") as BlockScript;
83      blockScript.Initialize(x,y,z,this);
84     }
85    }
86   }
87
88   // instantiate the connectors
89   for (x = 0; x <= 2; x++) {
90    for (y = 0; y <= 2; y++) {
91     for (z = 0; z <= 2; z++) {
92      //instantiate the x connector
93      connectorInstance = Instantiate (connector) as Transform;
94      connectorScript = connectorInstance.gameObject.GetComponent("ConnectorScript") as ConnectorScript;
95      connectorScript.Initialize(x,y,z,"x", this);
96      this.connectors[x,y,z,0] = connectorInstance;
97
98      //instantiate the y connector
99      connectorInstance = Instantiate (connector) as Transform;
100      connectorScript = connectorInstance.gameObject.GetComponent("ConnectorScript") as ConnectorScript;
101      connectorScript.Initialize(x,y,z,"y", this);
102      this.connectors[x,y,z,1] = connectorInstance;
103
104      //instantiate the z connector
105      connectorInstance = Instantiate (connector) as Transform;
106      connectorScript = connectorInstance.gameObject.GetComponent("ConnectorScript") as ConnectorScript;
107      connectorScript.Initialize(x,y,z,"z", this);
108      this.connectors[x,y,z,2] = connectorInstance;
109     }
110    }
111   }
112
113   //instantiate the move blocks
114   if (PlayerPrefs.HasKey ("game_status")) {
115    this.loadSavedGame();
116   }
117   else {
118    this.restart ();
119   }
120  }

TextMesh 160 lượt xem

Gõ tìm kiếm nhanh...