블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2328)N
Unity3D (572)N
Programming (472)
Unreal (4)
Gamebryo (56)
Tip & Tech (185)
협업 (34)
3DS Max (3)
Game (12)
Utility (116)
Etc (92)
Link (31)
Portfolio (19)
Subject (90)
iOS,OSX (38)
Android (13)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (1)
Memories (19)
Interest (37)
Thinking (36)
한글 (26)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (19)
Exercise (3)
나만의 맛집 (2)
냥이 (9)
육아 (9)
Total1,346,280
Today86
Yesterday132
Statistics Graph

달력

« » 2019.12
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        

공지사항

태그목록

I was working on a library in Unity that was using a lot of 3D text and therefore was requiring a lot of draw calls. This got me thinking, since all the pieces of text were on the same orientation plane would there be a way instead to draw all the text to a one texture and render that instead. Another benefit of doing this would be you could then apply text at runtime to a non-flat mesh via a texture. This would allow you to do cool things like personalizing a player’s jersey with the user’s name, localizing complex in-game advertisements, etc. So I decided to look into this. What started as a few experiments turned into not so small project.

Doing research I found a way of achieving this using Render Texture. What you could do was by using either with GUI text or 3D Text place a camera to capture a single frame and render it to a texture. While not completely straightforward or easy it does work and allows quite a bit of flexibility on what you are creating. This method does come with a drawback, it needs Render Texture support. Only Unity Pro supports Render Texture, plus they are not supported on the iPhone (Unity Feature Breakdown).

Since my goal was to create a library that all Unity developers could use, this was not an acceptable solution.

My next idea was to manually draw the text to a texture using SetPixels. While the performance of writing to a texture using SetPixels is less than ideal, I only needed to do this once.
Note: The reason why this is so slow is after calling setPixel(s) you have to call Apply which send the whole texture to the GPU. The Apply method is unfortunately very slow, but is “usable” if you are not needing to call it repeatedly.

I thought developing this using this method shouldn’t be too bad as I knew when you import a font into Unity, Unity automatically creates a texture containing the font. What I didn’t anticipate was that the letters were not in a grid format, but places tightly together. With no runtime access to letter position data, this was not going to work.

Next on my list was Custom Font, a feature where you can “easily” create your own font in Unity. With Custom Font you supply your own font image laid out in a grid. Using this method I could determine where the letters were positioned. I now needed a way create a font image in a grid pattern and get all the Per Character Kerning values from a font.

In digging through the internet I came across a few mentions of something called the Unity Font Mapper. It took me a very long time but I finally tracked down a link to the app: http://loewald.com/unity/unityfontmapper.zip. This Mac application not only generates the font image in a grid pattern, but generates a file containing the Per Character Kerning information.

Once I had a Custom Font working I then discovered there is no programming access to any of its properties (I still use Custom Font as part of my final solution in hopes at some point access will be opened up to those values). This means these values now needed to be entered twice if you want to use the same font for both a Text-to-Texture font and as a regular font.

I now had all the pieces and it was just a matter of putting it all together. While the current version of this code is far from perfect and is missing features like colored text it does exactly what I wanted it to do.

To check out the demo click here.

To get the source (unitypackage) click here.

To get only the TextToTexture class (TextToTexture.cs) click here.

Source Requirements: Unity 2.6

Setup Steps

  1. Create a font image in a grid pattern and collect kerning values (either manually or using the Unity Font Mapper)
  2. Import the font image(texture) into Unity and select ‘Build Alpha From Grayscale’ and set ‘Is Readable’ to true.
  3. Add this texture to a material
  4. Within Unity create a Custom Font. If you aren’t wanting to use this font for anything but Text-to-Texture you can just setup the basics and ignore the Per Character Kerning values within the Custom Font (these take a long time to enter)
  5. Create a script that calls TextToTexture and passes all the required information (example in source: AddTextToTexture.cs)

Note: I used a Decal material in this demo so the text would appear on top of the primary texture. But you can use any material that you wish.



출처 : http://blog.almostlogical.com/2010/08/20/adding-text-to-texture-at-runtime-in-unity3d-without-using-render-texture/

Posted by blueasa

댓글을 달아 주세요

유니티3D에서 text 파일 읽고/쓰는 소스코드 몇가지를 올립니다. 
이거 잘 안되서 저는 엄청 혼났거든요. 
아래 소스들 중 골라서 사용해보세요. 

1. c#코드, FileInfo 클래스 사용하는게 특징이네요. 
파일 읽기는 부분만 있습니다. 

using System; 
using System.IO; 

public class LineReader : MonoBehaviour 

    protected FileInfo theSourceFile = null; 
    protected StreamReader reader = null; 
    protected string text = " "; // assigned to allow first line to be read below 

    void Start () { 
        theSourceFile = new FileInfo ("Test.txt"); 
        reader = theSourceFile.OpenText(); 
    } 
    
    void Update () { 
        if (text != null) { 
            text = reader.ReadLine(); 
            //Console.WriteLine(text); 
            print (text); 
        } 
    } 


******* 
2. javascript 코드, 
파일 읽고/쓰는 함수가 다 있습니다. 
읽을 때 File 클래스를 사용합니다. 

import System.IO; 
var filePath = "/Users/ResetOfDirectoryPath/testWrite.txt"; 

function Update() { 
if (Input.GetKeyDown("r")) { 
WriteFile(filePath); 

if (Input.GetKeyDown("f")) { 
ReadFile(filePath); 



function WriteFile(filepathIncludingFileName : String) 

var sw : StreamWriter = new StreamWriter(filepathIncludingFileName); 
sw.WriteLine("Line to write"); 
sw.WriteLine("Another Line"); 
sw.Flush(); 
sw.Close(); 


function ReadFile(filepathIncludingFileName : String) { 
sr = new File.OpenText(filepathIncludingFileName); 

input = ""; 
while (true) { 
input = sr.ReadLine(); 
if (input == null) { break; } 
Debug.Log("line="+input); 

sr.Close(); 


****** 
3. javascript 코드, 파일 읽기는 부분만인데 
읽을 때 StreamReader 클래스를 이용합니다. 

import System.IO; 
var fileName = "foo.txt"; 
function Start () 

    var sr = new StreamReader(Application.dataPath + "/" + fileName); 
    var fileContents = sr.ReadToEnd(); 
    sr.Close(); 
    var lines = fileContents.Split("\n"[0]); 
    for (line in lines) { 
        print (line); 
    } 


***** 
4. 기타 
이 부분은 저도 테스트를 못했습니다. 
var pathToFile = "path/to/example.txt"; 
var url = "file://" + pathToFile; 
yiel download = new WWW(url); 
text = download.data; 

**** 
여기 나온 코드들은 unity3d 포럼에 나와있는 코드들인데 개발 시에 사용하려고 복사해뒀던 것들입니다.



출처 : http://www.applclub.com/bbs/board.php?bo_table=F&wr_id=1170&sca=%B0%AD%C1%C2%2F%C6%C1

'Unity3D' 카테고리의 다른 글

Inspector에 다차원 배열 값 셋팅하기(for C#)  (1) 2012.10.24
Unity3D Scripting Overview  (0) 2012.10.24
text 파일 Read/Write 하는 소스 몇가지  (0) 2012.10.22
에셋 서버 Scene Merge 관련..  (0) 2012.10.19
유니티 관련  (0) 2012.10.17
Transform Custom Editor  (0) 2012.10.15
Posted by blueasa

댓글을 달아 주세요


richTextBox에 클립보드로부터 붙이기를 할 때 rtf 포맷으로 붙이기가 될 때가 있다. 그런데, 지금 하려는

작업이 plain text를 붙여야만 하는 경우여서 구글링해봤으나.. 결국은 속임수를 쓰기로...
방법은 richTextBox에서 Ctrl+V 키보드 이벤트를 가로채서 클립보드의 텍스트만 집어넣는 것이다. 간단?

ㅋㅋ 코드는 다음과 같다.


private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if ((e.KeyCode == Keys.V) && (e.Modifiers == Keys.Control))
    {
        e.Handled = true;
        richTextBox1.SelectedText = Clipboard.GetText();
    }
}

출처 : 모름 -ㅅ-;;

Posted by blueasa

댓글을 달아 주세요