Version: Unity 6.2 Alpha (6000.2)
LanguageEnglish
  • C#

SerializedProperty.stringValue

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public string stringValue;

Description

Value of a string property.

Contains a valid value only if propertyType is SerializedPropertyType.String, otherwise accessing it results in an error logged.

Additional resources: propertyType, SerializedPropertyType.String.

using UnityEngine;
using UnityEditor;

public class SerializedPropertyStringExample : ScriptableObject { public string m_string;

[MenuItem("Example/SerializedProperty stringValue Set Example")] static void StringValueExample() { var scriptableObject = ScriptableObject.CreateInstance<SerializedPropertyStringExample>(); using (var serializedObject = new SerializedObject(scriptableObject)) { SerializedProperty stringField = serializedObject.FindProperty("m_string"); stringField.stringValue = "Hello World"; serializedObject.ApplyModifiedProperties(); }

// Save the ScriptableObject into the Project. // Viewing this Asset in the inspector will show the value of m_string as "Hello World" AssetDatabase.CreateAsset(scriptableObject, "Assets/Editor/SerializedPropertyStringExample.asset"); } }
using UnityEditor;
using UnityEngine;

public class SerializedPropertyStringExample2 { [MenuItem("Example/SerializedProperty stringValue Get Example")] public static void PrintStringProperties() { // Retrieve the currently selected Scene object in the Hierarchy Window, // or Asset selected in the project view var selectedObject = Selection.activeObject; if (selectedObject == null) { Debug.Log("No object selected."); return; }

SerializedObject serializedObject = new SerializedObject(selectedObject);

// Get the base property SerializedProperty property = serializedObject.GetIterator();

// Move to the first property bool visitChildren = true; property.NextVisible(visitChildren);

visitChildren = false;

// Iterate through all the top level properties (not recursive) do { if (property.propertyType == SerializedPropertyType.String) { Debug.Log($"Property: {property.name}, Value: {property.stringValue}"); } } while (property.NextVisible(false)); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961