Unity3d開發之十四:unity3d長string格式化,並且滑動顯示,指定替換文字內容

笑臉人發表於2020-10-22

長文字顯示-----------------------------------------------------------------------------

宣告一個string型別,記住格式 @“文字 ”
例項如下:

string detail = @"
1  .This activity is valid from {0} to {1}. You have to use {2} App Client (the ""App"")to join in this activity.
2  .During the activity, you may get different kinds of Puzzles and redeem them for the following rewards:
{3}
3   .During the activity, you may get the Puzzles in the following ways:
(1)  Bubble for Puzzles.You may get random Puzzles when take part in Bubble for Puzzles.There are six Bubbles at the same time displayed in applicable page as well as the kind of the Puzzle(s) you may get for each Bubble.You may choose the Bubble you want and watch a video from the beginning to the end according to the instruction to get a random number of the Puzzle(s) of such kind. Please note that if you close the App Or turn off the screen before you finish watching the video, you will get nothing; 
(2)  Luck Spin.You may get random Puzzles when you spin in the Lucky Spin.To get a chance of spin, you shall watch a video from the beginning to the end according to the instruction.
4  .The ways you get the Puzzles, the number of Puzzles you get in each way and the rewards may be changed from time to time. Please refer to the activity page in the App to check the latest activity details.
5  .When you redeem for a reward (except for the Coins), you must fill in your shipping information accurately, otherwise you are solely responsible for the failure of reward delivery.We will confirm your shipping information within 7 working days.After our confirmation, the reward will be delivered to you in several days. You need to abide by all applicable laws and take responsibility for tax declaration and payment.
6  .Please note that upon the end of the activity, any unredeemed Puzzles will be cleared up and no compensation will be made to you.
7  .We will not provide any after-sales services for the rewards you get in the App. If you have any question about the rewards, please contact the applicable manufacturer.
8  .In the event of any force majeure event or any change of circumstance that we cannot control(including but not limited to strikes, act of God, policy changes, act of governmental, cyberattack and system failure) leading to the cessation or adjustment of any activity, please understand that we are not obligated to make any compensation to you, nor shall we undertake any liability whatsoever.In the event of any failure to whatsoever.In the event of any failure to participate in any activity due to your improper operation, network failure, failure of telecom operator or any third party reasons, we are not obligated to make any compensation, nor shall we undertake any legal liability whatsoever.
9  .Please strictly abide by the activity rules.If any cheating or violation is found(including but not limited to using illegal tools to decode our services and hackings), measures will be taken including but not limited to disqualification from taking part in this activity, redeeming for any rewards, getting any redeemed reward and/or using the App, under which circumstance, no compensation in any form will be made to you and we also reserve the right to require you to return any rights, interests and rewards that you have received.
10   .To the extent permitted by the applicable law, we have the right to make adjustments to the activity rules at any time, and we will send prior notice in the App if we decide to make significant adjustment. The adjustment will take effect at the time of the release of the notice or the time specified in the notice. If you do not agree with any adjustments please do not join in this activity anymore.
11   .We reserve the right to interpret the activity rules to the extent permitted by applicable laws and regulations.
12   .Our End User License Agreement and Privacy Policy shall also be applicable to this activity.In case of any difference and inconsistency between the policies and any specific activity rules, the specific activity rules shall prevail in terms of this activity.
13   .If you have any questions or disputes arising out of or in relation to the rules this activity or the App please contact us by emailing at {4}.
14   .Apple is not involved in any way with this activity.
";

展示:

m_Email.text = m_DetailData.email;

滑動顯示-----------------------------------------------------------------------------

新增滑動列表:
在這裡插入圖片描述
為滑動列表新增自動擴充的元件:
在這裡插入圖片描述
為文字框新增自動擴充元件:
在這裡插入圖片描述
程式碼裡面匹配文字框高度和滑動列表高度,這裡需要注意的就是需要用協程做個延時,0.1s就行,不然會有匹配不了高度的bug,這可能是unity自帶的毛病:

            StartCoroutine(TimeUtlis.Delay(() =>
            {
                float height = m_Texts.GetComponent<RectTransform>().rect.height;
                m_content.GetComponent<RectTransform>().SetHeight(height);
            }));

替換指定文字-----------------------------------------------------------------------------

主要使用的是 string.Format(替換的文字,文字中的指定位置{0},文字中的指定位置{1},文字中的指定位置{2},文字中的指定位置{3},文字中的指定位置{4})
類似:
在這裡插入圖片描述
如果需要手動換行,在指定位置使用 \n即可!

            string detailText = "";
            for (int i = 0; i < m_DetailData.detailItemDatas.Count; i++)
            {
                detailText += "("+(i+1)+")" +"  Redeem  " + m_DetailData.detailItemDatas[i].target +"  " + m_DetailData.detailItemDatas[i].title+  "  Puzzles for a  " + m_DetailData.detailItemDatas[i].title+"; \n";
            }
            m_Texts.text = string.Format(detail, m_DetailData.startDate, m_DetailData.endDate, m_DetailData.name, detailText, m_DetailData.email);

相關文章