unity eventtrigger怎么获取触发对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity eventtrigger怎么获取触发对象相关的知识,希望对你有一定的参考价值。

参考技术A 加个Button组件即可使用BaseEventData.SelectObject获取当前触发对象 参考技术B gameObject );


可以尝试看看这个鼠标点击事件,放在脚本与update同级就可以了。
新手,如果见解不当,请指正,多谢了void OnMouseDown()

Destroy (obj本回答被提问者采纳

WPF EventTrigger并不总是触发

我有一个WPF TreeView,我在视觉上定制看起来像这样:

treeview

我定义了一个EventTrigger来对点击做出反应:

<TreeView>            
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseLeftButtonUp">
            <i:InvokeCommandAction Command="{Binding OpenPartListCommand}" CommandParameter="{Binding ElementName=PartsTreeView, Path=SelectedItem}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>

    <TreeView.Resources>
        <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TreeViewItem" >
                            <Grid Margin="6,4">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>

                                <Border Name="Bd" 
                                        Background="{Binding Background}"
                                        BorderBrush="Black" 
                                        BorderThickness="1.2" 
                                        CornerRadius="0"
                                        MinHeight="35"
                                        Padding="5"     
                                        SnapsToDevicePixels="True"
                                        Margin="{Binding Path=Margin}" >
                                    <Grid>                                            
                                        <Expander Name="Exp" IsExpanded="{TemplateBinding TreeViewItem.IsExpanded}">
                                            <Expander.Header>                                                   
                                                <ContentPresenter ContentSource="Header"                                                                       
                                                                  Width="{Binding Path=ActualWidth, ElementName=PartsTreeView, Mode=OneWay, Converter={StaticResource MathConverter}, ConverterParameter=@VALUE-80}"  />

                                            </Expander.Header>                                                
                                            <ItemsPresenter />
                                        </Expander>

                                        <ContentPresenter Name="CntPres"
                                                    ContentSource="Header"
                                                    HorizontalAlignment="Center"
                                                    VerticalAlignment="Center"
                                                    Visibility="Collapsed"                                                         
                                                    Width="{Binding Path=ActualWidth, ElementName=PartsTreeView, Mode=OneWay, Converter={StaticResource MathConverter}, ConverterParameter=@VALUE-80}"  
                                                    />              
                                    </Grid>
                                </Border>
                            </Grid>

                            <ControlTemplate.Triggers>

                                <Trigger Property="TreeViewItem.HasItems" Value="false">
                                    <Setter 
                                          TargetName="Exp" 
                                          Property="Visibility" 
                                          Value="Collapsed" />
                                    <Setter 
                                          TargetName="CntPres" 
                                          Property="Visibility" 
                                          Value="Visible" />            
                                </Trigger>


                                <Trigger Property="TreeViewItem.Name" Value="IsSpecial">
                                    <Setter Property="Background" TargetName="Bd" Value="#FFC3AF"/>
                                </Trigger>
                                <Trigger Property="TreeViewItem.Name" Value="IsNotSpecial">
                                    <Setter Property="Background" TargetName="Bd" Value="#8BADC5"/>
                                </Trigger>


                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" TargetName="Bd" Value="#FFF0EA"/>
                                </Trigger>
                                <Trigger Property="IsFocused" Value="True">
                                    <Setter Property="Background" TargetName="Bd" Value="#FFF0EA"/>
                                </Trigger>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter TargetName="Bd" Property="Background" Value="#FFF0EA"/>
                                </Trigger>

                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>

                <Setter Property="ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <ItemsPanelTemplate.Resources>                                    
                            </ItemsPanelTemplate.Resources>
                            <StackPanel  IsItemsHost="True"   />
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    </TreeView.Resources>
</TreeView>

问题是单击根元素“Ausführungsvorschrift”只会扩展树,但事件不会被触发。它在单击子元素时触发,并在单击边框内部时触发。但是标题不会触发任何东西。

答案

尝试将ExpandedCollapsed事件的触发器添加到模板中的Expander元素:

<Expander Name="Exp" IsExpanded="{TemplateBinding TreeViewItem.IsExpanded}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Expanded">
            <i:InvokeCommandAction Command="{Binding DataContext.OpenPartListCommand, RelativeSource={RelativeSource AncestorType=TreeView}}" 
                                   CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}}" />
        </i:EventTrigger>
        <i:EventTrigger EventName="Collapsed">
            <i:InvokeCommandAction Command="{Binding DataContext.OpenPartListCommand, RelativeSource={RelativeSource AncestorType=TreeView}}" 
                                   CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Expander.Header>
        <ContentPresenter ContentSource="Header" />
    </Expander.Header>
    <ItemsPresenter />
</Expander>

以上是关于unity eventtrigger怎么获取触发对象的主要内容,如果未能解决你的问题,请参考以下文章

unityevent与持续按键触发

WPF EventTrigger并不总是触发

用ECMAScript4 ( ActionScript3) 实现Unity的热更新 -- 使用原型链和EventTrigger

EventTrigger 事件触发器

unity 手指旋转角色的功能

EventTrigger 无法在 IsEnabledChanged 上触发