2020年01月19日

The system cannot find the file specifiedでビルドエラーになる場合

Unityのバージョンを更新した場合などに次のようなビルドエラーになってしまうことがあるようです。

Win32Exception: ApplicationName='...\tools\bin\sdkmanager.bat', CommandLine='--list', CurrentDirectory='...', Native error= The system cannot find the file specified.

そういう場合はUnityを一旦終了させて、Libraryフォルダーを削除してみるとビルドできるようになることもあるみたいです。(自己責任ですが)困っている人は試してみるのも良いかなと思います。

...の部分は省略です。

リンク

Unity Issue Tracker - "The system cannot find file specified" error while opening the project for the first time
https://issuetracker.unity3d.com/issues/the-system-cannot-find-file-specified-error-while-opening-the-project-for-the-first-time
タグ:android
posted by unity-chan at 23:21 | TIPS

2020年01月12日

C#で文字列の分割

C#では、Splitというメソッドを使うことで簡単に文字列の分割ができるそうです。

使い方は次のような感じです。

コンマ「,」かハイフン「-」で分割

using System;

class Example1
{
static public void Main()
{
string str = "one,two-three";
char[] sep = { ',', '-' };

string[] ar = str.Split(sep, StringSplitOptions.None);

foreach(string s in ar) {
Console.WriteLine(s);
}
}
}

実行結果

one
two
three


連続した3つのハイフン「---」で分割

using System;

class Example2
{
static public void Main()
{
string str = "one---two-three";
string[] sep = { "---" };

string[] ar = str.Split(sep, StringSplitOptions.None);

foreach(string s in ar) {
Console.WriteLine(s);
}
}
}

実行結果

one
two-three

他にも正規表現を使って分割したりできるみたいです。

リンク

String.Split Method (System) | Microsoft Docs
https://docs.microsoft.com/en-us/dotnet/api/system.string.split
タグ:C# String
posted by unity-chan at 16:33 | Script

2020年01月11日

文字列に合わせたUI Textのサイズを取得する方法

UnityのUI.Textで、実際にテキストを表示した際のサイズが知りたい場合はpreferredWidthpreferredHeightを使って調べることができるそうです。

使い方

Text text;

float width = text.preferredWidth;
float height = text.preferredHeight;

テキストの長さに合わせてUIを調整したい場合に便利かなと思います。

リンク

Unity - Scripting API: ILayoutElement
https://docs.unity3d.com/ScriptReference/UI.ILayoutElement.html
posted by unity-chan at 15:52 | Script

2020年01月08日

allowSceneActivation=falseを使う場合の注意

SceneManager.LoadSceneAsync

allowSceneActivation = false

で使う場合は、isDonetrueにならないそうなので、気を付けてください。

allowSceneActivation = true

とした後でtrueになるそうです。ちなみに、progressの値も0.9までで止まるみたいです。

ロードの完了を待ってシーンを表示するコード書こうとして少しハマってしまいました。

リンク

Unity - Scripting API: AsyncOperation.allowSceneActivation
https://docs.unity3d.com/ScriptReference/AsyncOperation-allowSceneActivation.html

Unity - Scripting API: SceneManagement.SceneManager.LoadSceneAsync
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html
posted by unity-chan at 08:11 | Script

2020年01月01日

C#のプロパティーの簡潔な書き方

C#でプロパティーという書き方がありますが使っていますか?

float width
{
get { return size.width; }
}

みたいな書き方です。

これは次のような感じで省略して書くことができるみたいです。

float width => size.width;

setも使いたい場合は少し長くなりますが、次のような感じです。

float width
{
get => size.width;
set => set.width = value;
}

Visual Studioの表示で教えてもらったのですが、短かくて読み易くなったような気がします。

リンク

Properties - C# Programming Guide | Microsoft Docs
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
タグ:C#
posted by unity-chan at 16:08 | Script